gpt4 book ai didi

c# - RestSharp 在 POST 上默认 Content-Type 为 application/x-www-form-urlencoded

转载 作者:太空狗 更新时间:2023-10-29 21:08:31 28 4
gpt4 key购买 nike

RestSharp 似乎不允许我覆盖发布请求的内容类型。我已按照找到的说明进行操作 here无济于事。我还尝试通过 request.AddHeaders("content-type", "application/json"); 手动将 header 内容类型设置为 application/json;

请求执行示例:

private IRestResponse ExecuteRequest<T>(string resource, Method method, T model)
{
var client = CreateRestClient();
var request = new RestRequest(resource, method)
{
RequestFormat = DataFormat.Json
};
var json = JsonConvert.SerializeObject(model);

request.AddHeader("Accept", "application/json");
request.AddHeader("User-Agent", "Fiddler");
request.Parameters.Clear();
request.AddParameter("auth_token", _apiKey);
request.AddParameter("application/json", json, ParameteType.RequestBody);

return client.Execute(request);
}

响应错误信息:

{
"error": {
"code": 400,
"message": "The request requires a properly encoded body with the 'content-type' header set to '['application/json']",
"type": "Bad Request" }
}

Fiddler请求原始数据:

POST  **omitted** HTTP/1.1
Accept: application/json, application/xml, text/json, text/x-json,text/javascript, text/xml
User-Agent: RestSharp/105.0.1.0
Content-Type: application/x-www-form-urlencoded
Host: **omitted**
Content-Length: 51
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

可以看到,请求的Content-Type还是application/x-www-form-urlencoded。有任何想法吗? (提前致谢)

最佳答案

这似乎是对 RestSharp 如何解释 post 请求参数的误解。来自 John Sheehan 在 google group 上的帖子:

If it's a GET request, you can't have a request body and AddParameter adds values to the URL querystring. If it's a POST you can't include a POST parameter and a serialized request body since they occupy the same space. You could do a multipart POST body but this is not very common. Unfortunately if you're making a POST the only way to set the URL querystring value is through either string concatenation or UrlSegments:

var key = "12345";
var request = new RestRequest("api?key=" + key);
// or
var request = new RestRequest("api?key={key});
request.AddUrlSegment("key", "12345");

我现在可以使用的修改后的执行请求方法如下所示:

private IRestResponse ExecuteRequestAsPost<T>(T model, string resource, Method method)
{
resource += "?auth_token={token}";
var client = CreateRestClient();
var request = new RestRequest(resource, method) { RequestFormat = DataFormat.Json };
var json = JsonConvert.SerializeObject(model);
request.AddHeader("User-Agent", "Fiddler");

request.AddUrlSegment("token", _apiKey);
request.AddParameter("application/json", json, ParameterType.RequestBody);

return client.Execute(request);
}

关于c# - RestSharp 在 POST 上默认 Content-Type 为 application/x-www-form-urlencoded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28532677/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com