18

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?

ihatemash
  • 1,474
  • 3
  • 21
  • 42

3 Answers3

28

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

request.AddParameter("application/json", locationJSON, ParameterType.RequestBody);
Kevin Le
  • 846
  • 8
  • 17
  • 3
    This solution works when the API cal requires body instead of parameter. RestSharp appears to not view correctly though, as even doing this and viewing the request in the debugger, it shows the body as null, instead putting it in the parameter slot. There appears to be no way of knowing if the parameter is actually the body just from the request object. But all in all, this solution works great. – ATCraiger Feb 11 '21 at 19:45
13

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 ".

1

I have tried like this and it working fine, Add Bearer with token

 request.AddHeader("cache-control", "no-cache");
 request.AddHeader("authorization", "Bearer " + "your token key");
 request.AddHeader("accept", "application/json");
logeshpalani31
  • 1,416
  • 12
  • 33
PK-1825
  • 1,431
  • 19
  • 39