challengeAccepted
challengeAccepted

Reputation: 7590

AddBody for RestSharp is failing when serializing to JSON

I am trying to do AddBody for a restrequest post method. I am using RestSharp version 104.XXX which doesnt have addJsonBody like version 105.XXXX has because of other issues that my application faces. So I am stuck with version 104 and in the process I cannot use addBody and send a serialized json and everytime i use the below code, I get . "error":"Unsupported Media Type","message":"Content type 'text/xml;charset=UTF-8' not supported".. Has anyone came across this issue and figured out a way using old version of RestSharp..

  var restRequest = new RestRequest(Method.POST);
  restRequest.AddHeader("authorization",  "Bearer " + token);
  restRequest.AddHeader("source", "M");
  restRequest.AddHeader("content-type", "application/json");
  restRequest.AddBody(JsonConvert.SerializeObject(requestObj, this.jsonSettings));

Upvotes: 2

Views: 1092

Answers (1)

dvo
dvo

Reputation: 2153

You can specify that the body parameter is JSON by doing the following:

request.AddParameter("application/json", jsonString, ParameterType.RequestBody);

Your case:

restRequest.AddParameter("application/json", 
                         JsonConvert.SerializeObject(requestObj, this.jsonSettings), 
                         ParameterType.RequestBody);

Upvotes: 2

Related Questions