Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to POST raw JSON #703

Closed
captnrob opened this issue Jun 17, 2015 · 3 comments
Closed

Unable to POST raw JSON #703

captnrob opened this issue Jun 17, 2015 · 3 comments

Comments

@captnrob
Copy link

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;
@hallem
Copy link
Member

hallem commented Jun 19, 2015

Use request.AddJsonBody instead.

@hallem hallem closed this as completed Jun 19, 2015
@captnrob
Copy link
Author

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.

@Eternal21
Copy link

Eternal21 commented May 1, 2018

@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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants