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

标签 c# restsharp

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/

相关文章:

c# - ZeroMQ线程/任务建议

c# - 我怎样才能看到发送的实际原始请求

c# - MSBuild 内联任务 - 引用非标准 Microsoft 程序集

c# - 在 RestSharp 中导入下一组图像

c# - RestSharp 在上传时将整个文件加载到内存中。如何避免?

RestSharp:底层连接已关闭:服务器关闭了预期保持事件状态的连接

c# - 反序列化响应

c# - Azure 服务总线客户端连接持久性

c# - 图像按钮中的 asp.net 图像保持静止

c# - 如何使用WF4检索MVC5中的页面流逻辑