Restsharp PUT自定义标题

时间:2013-05-15 18:24:28

标签: c# .net restsharp

我必须使用RestSharp将一些数据输入API。

API资源是:/ clients / services / finances / {finances-id} / subcategory / {subcategory-id}

除模板参数外,还有一些查询参数: organization-id(字符串) operator-id(string)

此外,请求Content-Type必须是application / xml

我尝试使用RestSharp创建此PUT请求的方式:

 RestClient client = new RestClient(url);
 client.Authenticator = Auth1Authenticator.ForRequestToken(Config.getVal("api_key"), Config.getVal("secret_key"));
 IRestRequest request = new RestRequest("", Method.PUT);
 request.RequestFormat = DataFormat.Xml;
 request.AddParameter("organization-id", Config.getVal("params.org")); 
 request.AddParameter("operator-id", "Accounting");
 IRestResponse response = client.Execute(request);

但我只获得HTTP状态415 - 不支持的媒体类型

你能帮我解决一下吗? GET请求就像魅力一样。

1 个答案:

答案 0 :(得分:1)

尝试发送您的请求正文:

request.XmlSerializer = new RestSharp.Serializers.XmlSerializer();
request.RequestFormat = DataFormat.Xml;
request.AddBody([new instance of the object you want to send]); 

另外,您确定要访问的网址是否正确(即占位符是否填充了参数)?

或者您可以尝试这样做:

request.AddParameter("text/xml", [your object to serialize to xml], ParameterType.RequestBody);

您也可以尝试使您的示例尽可能与restsharp wiki示例类似,以使其工作:

https://github.com/restsharp/RestSharp/wiki/Recommended-Usage