Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add json to RestSharp POST request

Tags:

json

c#

restsharp

I have the following JSON string that is passed into my c# code as a string parameter - AddLocation(string locationJSON):

{"accountId":"57abb4d6aad4","address":{"city":"TEST","country":"TEST","postalCode":"TEST","state":"TEST","street":"TEST"},"alternateEmails":[{"email":"TEST"}],"alternatePhoneNumbers":[{"phoneNumber":"TEST"}],"alternateWebsites":[{"website":"TEST"}],"auditOnly":false,"busName":"593163b7-a465-43ea-b8fb-e5b967d9690c","email":"TEST EMAIL","primaryKeyword":"TEST","primaryPhone":"TEST","rankingKeywords":[{"keyword":"TEST","localArea":"TEST"}],"resellerLocationId":"5461caf7-f52f-4c2b-9089-2ir8hgdy62","website":"TEST"}

I'm trying to add the JSON to a RestSharp POST request like this but it's not working:

public string AddLocation(string locationJSON)
{
    var client = new RestClient(_authorizationDataProvider.LocationURL);
    var request = new RestRequest(Method.POST);
    request.RequestFormat = DataFormat.Json;
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("Authorization", _authorizationResponse.Token);
    ...
    request.AddJsonBody(locationJSON);
    var response = client.Execute(request);
}

The response comes back as "Bad Request". Here is what I get if I inspect the response in the debugger:

{"code":"invalid_json","details":{"obj.address":[{"msg":["error.path.missing"],"args":[]}],"obj.rankingKeywords":[{"msg":["error.path.missing"],"args":[]}],"obj.alternatePhoneNumbers":[{"msg":["error.path.missing"],"args":[]}],"obj.busName":[{"msg":["error.path.missing"],"args":[]}],"obj.accountId":[{"msg":["error.path.missing"],"args":[]}],"obj.alternateEmails":[{"msg":["error.path.missing"],"args":[]}],"obj.alternateWebsites":[{"msg":["error.path.missing"],"args":[]}],"obj.email":[{"msg":["error.path.missing"],"args":[]}],"obj.primaryKeyword":[{"msg":["error.path.missing"],"args":[]}],"obj.auditOnly":[{"msg":["error.path.missing"],"args":[]}]}}

I have inspected the request parameters after calling AddJsonBody and the value appears to be including the escape sequence for double quotes - which seems to be the issue.

{\"accountId\":\"57abb4d6aad4def3d213c25d\",\"address\":{\"city\":\"TEST\",\"country\":\"TEST\",\"postalCode\":\"TEST\",\"state\":\"TEST\",\"street\":\"TEST\"},\"alternateEmails\":[{\"email\":\"TEST\"}],\"alternatePhoneNumbers\":[{\"phoneNumber\":\"TEST\"}],\"alternateWebsites\":[{\"website\":\"TEST\"}],\"auditOnly\":false,\"busName\":\"84e7ef98-7a9f-4805-ab45-e852a4b078d8\",\"email\":\"TEST EMAIL\",\"primaryKeyword\":\"TEST\",\"primaryPhone\":\"TEST\",\"rankingKeywords\":[{\"keyword\":\"TEST\",\"localArea\":\"TEST\"}],\"resellerLocationId\":\"06b528a9-22a6-4853-8148-805c9cb46941\",\"website\":\"TEST\"}

so my question is how do I add a json string to the request body?

like image 894
ihatemash Avatar asked Aug 25 '16 19:08

ihatemash


People also ask

How do I send a post request on RestSharp?

var client = new RestClient("URL"); var request = new RestRequest("Resources",Method. POST); request. RequestFormat = RestSharp. DataFormat.

How do I request a RestSharp body?

The request body is a type of parameter. To add one, you can do one of these... req. AddBody(body); req.

Does RestSharp use HTTP client?

Since RestSharp uses the HttpClient, we should consider similar issues regarding the RestClient instantiation.


2 Answers

I've ran into this problem as well. Try something like this instead of AddJsonBody.

request.AddParameter("application/json", locationJSON, ParameterType.RequestBody);
like image 70
Kevin Le Avatar answered Oct 04 '22 21:10

Kevin Le


This should work:

request.AddParameter("application/json; charset=utf-8", JsonConvert.SerializeObject(yourObject), ParameterType.RequestBody);

If you directly add the serialized object, the problem is the Json convert is adding "\" before each ".

like image 41
SabariMurugan Sivakumar Avatar answered Oct 04 '22 22:10

SabariMurugan Sivakumar