Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API calls with RestSharp - prepends application/json to body causing null parameter on action

I have a Web API service that I'm trying to access via the console using RestSharp. My RestSharp code looks like this:

            RestClient client = new RestClient(baseUrlString);
        RestRequest request = new RestRequest("controllername/actionname");
        request.RequestFormat = DataFormat.Json;

        ProcessQuestion model = new ProcessQuestion()
        {
            Id = "123456",
            InstanceId = "123",
            UniqueId = "bfb16a18-d0d6-46ab-a5b3-2f0ebbfe8626",
            PostedAnswer = new Dictionary<string, string>() { { "question_7907_1", "selected" }, { "question_7907_2", "selected" } }
        };
        request.AddBody(model);

        var response = client.Execute(request)

My Web API action takes a model that has the same parameters as the above model. When the call executes, the binding fails and the parameter is null. I suspect this is due to the RestRequest.AddBody method prepending application/json to the body value as shown below:

{application/json={"Id":"123456","InstanceId":"123","UniqueId":"bfb16a18-d0d6-46ab-a5b3-2f0ebbfe8626","PostedAnswer":{"question_7907_1":"selected","question_7907_2":"selected"}}}

If I post using Fiddler the body binds to the model properly. Below is the body value I provided in Fiddler:

{'Id':'123456','InstanceId':'123','Uniqueid':'bfb16a18-d0d6-46ab-a5b3-2f0ebbfe8626','PostedAnswer':{'question_7907_1':'selected','question_7907_2':'selected'}}

Note that the body value in fiddler is the same with the exception of prepending the application/json key.

Also to note: I've tried what seems like everything...I've separated the parameters out in the action, used FromBody and FromUri attributes, tried custom DictionaryModelBinder's, tried custom ValueBinders, tried changing the way I'm using RestSharp (AddParameter with a RequestBody parameter, AddObject, different URL styles, etc.).

Has anyone else encountered this, and if so, did you solve it? I chose Web API for this service with hopes the model binding would work as it does in MVC, but I'm seeing that isn't the case.

Thanks

EDIT (resolved): RestSharp automatically uses the JsonSerializer for objects passed in the AddBody method. I figured I was missing something simple, and indeed I was... Adding the Method.Post parameter to the RestRequest instantiation solved the problem.

like image 758
Eric C. Bohn Avatar asked May 10 '13 16:05

Eric C. Bohn


1 Answers

  1. Specify the method when creating the request:

    RestRequest request = new RestRequest("controllername/actionname", Method.POST);

  2. Not sure what the default serializer is for body - you can try making it explicit:

    request.AddBody(request.JsonSerializer.Serialize(model));

  3. I'm not sure where the 'application/json' is coming from - that's the Content-Type header you should be sending with your request, definitely not part of the body. So do this instead:

    request.AddHeader("Content-type", "application/json; charset=utf-8");

  4. If this doesn't help, try making your code as similar to the example on their site as possible. Try removing complexity (even if it means changing the required functionality) - get it to a point when it works and build additional functionality on that.

http://restsharp.org/

like image 96
Joanna Derks Avatar answered Oct 14 '22 00:10

Joanna Derks