我正在为RSS阅读服务构建客户端。我正在使用RestSharp库与其API进行交互。使用RestSharp设置'Content-Type'标题
的API状态:
当创建或更新您必须设置应用程序/ JSON的记录; charset = utf-8作为Content-Type标头。
这是我的代码如下所示:
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:请使用'Content-Type:application/json; charset = utf-8'标题。”为什么RestSharp不通过标题?
我对RestSharp不熟悉,但我会使用Fiddler检查请求以确定RestSharp IS传递的是什么。可能是Content-Type头已经被添加了,你需要替换或删除/添加它。 我会假设'request.RequestFormat = DataFormat.Json'为你设置Content-Type标题。 –
我试过了,没有这条线。唯一的选择是JSON或XML。 –
请发布请求在Fiddler中的外观,它会告诉我们是否添加了Content-Type,而不是仅仅猜测。 – Darius