0
votes

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.

1
Where is the content that you are sending? I can't see anything added to the body of the request - for now all you've got is url path + query string. - Joanna Derks
It's asking for an XML, I have the schema but I don't know how to send it together with request. - HTB222

1 Answers

1
votes

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 (i.e. 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