C# 如何向RestSharp POST请求添加json

C# 如何向RestSharp POST请求添加json,c#,json,restsharp,C#,Json,Restsharp,我有以下JSON字符串作为字符串参数-AddLocation(string locationJSON)传递到我的c#代码中: 我正试图像这样将JSON添加到RestSharp POST请求中,但它不起作用: public string AddLocation(string locationJSON) { var client = new RestClient(_authorizationDataProvider.LocationURL); var request = new Re

我有以下JSON字符串作为字符串参数-AddLocation(string locationJSON)传递到我的c#代码中:

我正试图像这样将JSON添加到RestSharp POST请求中,但它不起作用:

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);
}
响应返回为“错误请求”。以下是我在调试器中检查响应时得到的结果:

{"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":[]}]}}
我在调用AddJsonBody后检查了请求参数,该值似乎包含双引号的转义序列——这似乎是问题所在

{\"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\"}

因此,我的问题是如何将json字符串添加到请求正文中?

我也遇到了这个问题。尝试类似的方法,而不是
AddJsonBody

request.AddParameter("application/json", locationJSON, ParameterType.RequestBody);
这应该起作用:

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

如果直接添加序列化对象,问题是Json转换在每个对象之前添加“\”。

我已经尝试过这样做,效果很好,使用令牌添加承载

 request.AddHeader("cache-control", "no-cache");
 request.AddHeader("authorization", "Bearer " + "your token key");
 request.AddHeader("accept", "application/json");

当API cal需要主体而不是参数时,此解决方案可以工作。RestSharp似乎无法正确查看,尽管如此,即使这样做并在调试器中查看请求,它也会将主体显示为null,而不是将其放入参数槽中。似乎无法仅从请求o知道参数是否实际上是主体但总而言之,这个解决方案非常有效。
 request.AddHeader("cache-control", "no-cache");
 request.AddHeader("authorization", "Bearer " + "your token key");
 request.AddHeader("accept", "application/json");