简体   繁体   中英

Restsharp PUT custom header

I have to use RestSharp to PUT some data into an API.

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

Apart from template parameters, there are some query parameters: organization-id (string) operator-id (string)

And also, the request Content-Type must be application/xml

The way I'm trying to create this PUT request using RestSharp:

 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

Can you please help me to resolve this. GET request is working like a charm.

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)?

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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