簡體   English   中英

如何將 json 添加到 RestSharp POST 請求

[英]How to add json to RestSharp POST request

我有以下 JSON 字符串作為字符串參數傳遞到我的 c# 代碼中 - 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"}

我正在嘗試將 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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM