设置“的Content-Type”头使用RestSharp [英] Set 'Content-Type' header using RestSharp
问题描述
我建立一个客户端的RSS阅读服务。我使用的是 RestSharp 库与他们的API进行交互。
I'm building a client for an RSS reading service. I'm using the RestSharp library to interact with their API.
该API的状态:
在创建或更新您必须设置应用程序/ JSON的记录;
的charset = UTF-8作为Content-Type头
When creating or updating a record you must set application/json; charset=utf-8 as the Content-Type header.
这是我的代码是什么样子:
This is what my code looks like:
RestRequest request = new RestRequest("/v2/starred_entries.json", Method.POST);
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.RequestFormat = DataFormat.Json;
request.AddParameter("starred_entries", id);
//Pass the request to the RestSharp client
Messagebox.Show(rest.ExecuteAsPost(request, "POST").Content);
不过,该服务返回的错误415:请使用内容类型:应用程序/ JSON的;字符集= utf-8'。头?的为什么不是RestSharp传递头
However; the service is returning "Error 415: Please use the 'Content-Type: application/json; charset=utf-8' header." Why isn't RestSharp passing the header?
推荐答案
提供的我的博客是不超过RestSharp的1.02版本测试。如果你对我的回答提交评论与您的具体问题,我的解决方案,我可以更新它。
The solution provided on my blog is not tested beyond version 1.02 of RestSharp. If you submit a comment on my answer with your specific issue with my solution, I can update it.
var client = new RestClient("http://www.example.com/where/else?key=value");
var request = new RestRequest();
request.Method = Method.POST;
request.AddHeader("Accept", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json", strJSONContent, ParameterType.RequestBody);
var response = client.Execute(request);
这篇关于设置“的Content-Type”头使用RestSharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!