当我有此行时,为什么RestSharp发布表单名称/值对而不是JSON:`request.RequestFormat = DataFormat.Json;
var request = new RestRequest($"api/Users/{userId}/UpdateProperty", Method.PUT);
request.RequestFormat = DataFormat.Json;
request.AddObject(new { key = key, value = value });
Execute(request);
这将导致以下http请求(已使用Fiddler选中):
PUT /api/Users/c8c946f9-e1dd-49c6-9c7f-23572017058a/UpdateProperty HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 23
Accept-Encoding: gzip, deflate
key=Gender&value=Female
我期望正文为JSON:
{
key: "Gender",
value: "Female"
}
我在做什么错了?
答案 0 :(得分:0)
您要使用AddJsonBody方法而不是AddObject方法。您可能还想添加带有“ application / json”值的“ Content-type”标头。
基本上是这样的:
var request = new RestRequest($"api/Users/{userId}/UpdateProperty", Method.PUT);
request.AddHeader("Content-type", "application/json");
request.RequestFormat = DataFormat.Json;
request.AddJsonBody(new { key = key, value = value });
Execute(request);