重新设置PUT自定义标头

本文关键字:PUT 自定义 设置 新设置 | 更新日期: 2023-09-27 18:13:39

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

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

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

请求的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请求工作得很好

重新设置PUT自定义标头

尝试像这样发送请求正文:

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

另外,你确定你正在访问的URL是正确的(即占位符是否已填满你的参数)?

或者你可以试着这样做:

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

你也可以试着让你的例子尽可能地与restsharp wiki的例子相似,以使它工作:

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