RestSharp 在 POST 上默认 Content

编程入门 行业动态 更新时间:2024-10-23 15:24:47
本文介绍了RestSharp 在 POST 上默认 Content-Type 为 application/x-www-form-urlencoded的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

RestSharp 似乎不允许我覆盖发布请求的 Content-Type.我已按照此处找到的说明操作徒劳无功.我还尝试通过 request.AddHeaders("content-type", "application/json"); 手动将标题内容类型设置为 application/json

请求执行示例:

私有IRestResponse ExecuteRequest(字符串资源,方法方法,T模型){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);返回客户端.执行(请求);}

响应错误信息:

{错误": {代码":400,"message": "该请求需要一个正确编码的正文,其中 'content-type' 标头设置为 '['application/json']",类型":错误请求"}}

Fiddler 请求原始数据:

POST **省略** HTTP/1.1接受:application/json、application/xml、text/json、text/x-json、text/javascript、text/xml用户代理:RestSharp/105.0.1.0内容类型:应用程序/x-www-form-urlencoded主持人:**省略**内容长度:51接受编码:gzip、deflate连接:保持活动

如您所见,请求 Content-Type 仍然是 application/x-www-form-urlencoded.有任何想法吗?(提前致谢)

解决方案

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

如果是GET请求,则不能有请求体和AddParameter向 URL 查询字符串添加值.如果是 POST,则不能包含POST 参数和序列化的请求体,因为它们占用了相同的空间.你可以做一个多部分的 POST 正文,但这不是很常见的.不幸的是,如果您将 POST 作为设置URL 查询字符串值是通过字符串连接或网址段:

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

我修改后的 Execute 请求方法现在可以使用了:

私有IRestResponse ExecuteRequestAsPost(T模型,字符串资源,Method方法){资源 += "?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);返回客户端.执行(请求);}

RestSharp seems to not allow me to override the Content-Type for a post request. I've followed the directions found here to no avail. I've also tried manually setting the header content type to application/json via request.AddHeaders("content-type", "application/json");

Examples of the request execution:

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); }

The response error message:

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

Fiddler request raw data:

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

As you can see, the request Content-Type is still application/x-www-form-urlencoded. Any ideas? (thanks in advance)

解决方案

It appears this is a misunderstanding of how RestSharp interprets parameters for post requests. From John Sheehan's post on the 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");

My revised Execute request method that now works looks like this:

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); }

更多推荐

RestSharp 在 POST 上默认 Content

本文发布于:2023-10-09 18:22:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476431.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:RestSharp   POST   Content

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!