Restsharp: Unable to POST raw JSON

Created on 17 Jun 2015  路  3Comments  路  Source: restsharp/RestSharp

I am new to RESTSharp - and relatively new to REST in general. I have tested this service call in Postman and it works fine there with this same input but only when the json string is passed as raw text.

Service returns an Internal Server Error: "Expected parameter "Policy" not passed".

            string postData = @"{""Policy"":{""PolicyId"": 123456, ""Holder"": ""TestHolder Name"", ""HolderAddress"": {  ""Line1"": ""Address1"",  ""Line2"": ""address2"", ""City"": ""MyCity"", ""State"": ""ME"", ""Zip"": ""04079""}}}";

            var client = new RestClient(DecisionsSettings.DecisionsURL);
            client.AddDefaultHeader("Content-Type", "application/json");

            var request = new RestRequest("/decisions/Primary/?RuleId=" + _RuleId + "&Action=api&outputtype=JSON", Method.POST);

            request.AddParameter("userid", DecisionsSettings.DecisionsUsername);
            request.AddParameter("password", DecisionsSettings.DecisionsPassword);
            request.RequestFormat = DataFormat.Json;

           // request.AddBody(postData);//this did not work
            request.AddParameter("application/json; charset=utf-8", postData, ParameterType.RequestBody);



            var response = client.Execute(request);
            responseString = response.Content;

Most helpful comment

@hallem Using AddJsonBody will not work, since it expects a json object. As of right now there is no way to pass in a raw json string (AddBody, and setting content to Json does not work). The workaround is getting the raw string, deserializing into a json object, and then passing that to Restsharp AddJsonBody. Definitely a wasteful workaround since Restsharp serializes back into a string that we had to begin with.

EDIT: After comparing code for AddJsonBody() vs AddBody(), the following is the workaround that can be used to add raw JSON to a request body:

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

All 3 comments

Use request.AddJsonBody instead.

Tried that - it deserializes it even though it is already deserialized.
I did find the solution. Had to use content-type = "text/plain" to send raw text. The fact that the text was formatted as json apparently doesn't matter.

@hallem Using AddJsonBody will not work, since it expects a json object. As of right now there is no way to pass in a raw json string (AddBody, and setting content to Json does not work). The workaround is getting the raw string, deserializing into a json object, and then passing that to Restsharp AddJsonBody. Definitely a wasteful workaround since Restsharp serializes back into a string that we had to begin with.

EDIT: After comparing code for AddJsonBody() vs AddBody(), the following is the workaround that can be used to add raw JSON to a request body:

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nilsga picture nilsga  路  5Comments

abishekrsrikaanth picture abishekrsrikaanth  路  3Comments

maximuss picture maximuss  路  3Comments

thomasd3 picture thomasd3  路  5Comments

ChenJasonGit picture ChenJasonGit  路  5Comments