RestSharp “Unexpected Content-Type 'application/x-www-form-urlencoded', expecting 'application/json'


I'm trying to pass JSON data with POST to insert it in my mongoDB using RestSharp but I'm always having this error: "Unexpected Content-Type 'application/x-www-form-urlencoded', expecting 'application/json'.".

I tried to serialize it myself, then use request.AddBody(mySerializedObject) but it didn't work.

Then I tried to let RestSharp serializing it with

request.RequestFormat = DataFormat.Json; if(query != null) request.AddBody(myObject);

but it wasn't a success either.

Finally I tried this request.AddParameter("application/json", query, ParameterType.RequestBody);
and it's still not working.

So I think I'm not looking correctly at the issue, does somebody has an idea ?

Here is the code:

`public String GetHttpData(String collection, Method method, String query, Boolean findOne = false)
var uri = "";

    var client = new RestClient(uri);

    var request = new RestRequest("api/1/databases/{db}/collections/{coll}" , method);
    request.AddParameter("fo", findOne);
    request.AddParameter("apiKey", Common.API_KEY);
    request.AddUrlSegment("db", Common.DB_NAME);
    request.AddUrlSegment("coll", collection);
    request.AddHeader("Content-Type", "application/json; charset=utf-8");
    request.AddParameter("application/json", query, ParameterType.RequestBody);

    IRestResponse response = client.Execute(request);

    if (response.StatusCode == System.Net.HttpStatusCode.OK)
        return response.Content;
        throw (response.ErrorException);
Sign In or Register to comment.