简体   繁体   English

Restsharp PUT自定义标头

[英]Restsharp PUT custom header

I have to use RestSharp to PUT some data into an API. 我必须使用RestSharp将一些数据放入API。

The API resource is: /clients/services/finances/{finances-id}/subcategory/{subcategory-id} API资源为:/ clients / services / finances / {finances-id} / subcategory / {subcategory-id}

Apart from template parameters, there are some query parameters: organization-id (string) operator-id (string) 除了模板参数外,还有一些查询参数:organization-id(字符串)operator-id(字符串)

And also, the request Content-Type must be application/xml 而且,请求的Content-Type必须为application / xml

The way I'm trying to create this PUT request using RestSharp: 我尝试使用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);

But I'm only get HTTP Status 415 - Unsupported Media Type 但是我只有HTTP状态415-不支持的媒体类型

Can you please help me to resolve this. 您能帮我解决这个问题吗? GET request is working like a charm. GET请求的工作原理很简单。

Try sending your request body like this: 尝试像这样发送您的请求正文:

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

Also, are you sure that the URL you're accessing is correct (ie have the placeholders been filled with your params)? 另外,您确定要访问的URL是正确的(即,占位符已填充您的参数)吗?

Alternatively you can try doing this: 另外,您可以尝试执行以下操作:

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

You might also try and make your example as similar to the restsharp wiki example as possible to make it work: 您也可以尝试使示例与restsharp Wiki示例相似,以使其起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM