是否RestSharp覆盖手动设置Content-Type的? [英] Does RestSharp overwrite manually set Content-Type?

查看:1789
本文介绍了是否RestSharp覆盖手动设置Content-Type的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建通过RestSharp.RestRequest:

I'm creating a RestSharp.RestRequest via:

RestRequest request = new RestRequest();
request.Method = Method.POST;
request.Resource = "/rest-uri";

request.AddHeader("Content-Type", "application/someContentType");

string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + Environment.NewLine +
             "<register-request">" + Environment.NewLine +
             "    <name=\"someName\"/>" + Environment.NewLine +
             "</register-request>");

request.AddParameter("text/xml", registerSinkRequest, ParameterType.RequestBody);

(内容类型是手动设置为应用程序/ someContentType

(The Content-Type is manually set to application/someContentType)

在调试模式下,它也说明的Content-Type =应用程序/ someContentType

In debug-mode it also shows Content-Type=application/someContentType

但执行RestRequest返回 415媒体不支持 - 错误和Wireshark表明媒体类型设置为文本/ XML (如在AddParameter法集)。

But executing the RestRequest returns an 415 Media Not Supported-Error and WireShark shows that the Media-Type is set to text/xml (like set in the AddParameter-Method).

为什么RestSharp呈现出不同的内容类型,然后是Wireshark?我怎样才能防止内容类型来被改变(如果是)?

Why is RestSharp showing a different Content-Type then WireShark? And how can I prevent the Content-Type to be changed (if it is)?

推荐答案

svick的评论是正确的。在第一个参数设置内容类型 AddParameter()键,你可以离开了的AddHeader()电话。

svick's comment is right. Set the content type in the first parameter of AddParameter() and you can leave out the AddHeader() call.

虽然这是正确的答案,我会解释为什么它有这样做的,这不是正是明显的一个令人困惑的方法。

While that's the 'right' answer, I'll explain why it has a confusing method for doing this that's not exactly obvious.

要做到这一点是预期的方式使用 AddBody() RestRequest.RequestFormat 一起。举个例子:

The intended way to accomplish this is to use AddBody() along with RestRequest.RequestFormat. An example:

var client = new RestClient();
// client.XmlSerializer = new XmlSerializer(); // default
// client.XmlSerializer = new SuperXmlSerializer(); // can override with any implementaiton of ISerializer

var request = new RestRequest();
request.RequestFormat = DataFormat.Xml;
request.AddBody(objectToSerialize);

objectToSerialize 是基于序列化注册的XmlSerializer 。如果你使用 RequestFormat = DataFormat.Json ,然后用 RestClient.JsonSerializer ISerializer (你可以用它来覆盖默认的序列化)声明自己的内容类型是什么获取通过janky传递的实现 AddParameter() 你使用过载。

The serialization of objectToSerialize is based on the registered XmlSerializer. If you use RequestFormat = DataFormat.Json, then the RestClient.JsonSerializer is used. Implementations of ISerializer (which you can use to override the default serialization) declare their own Content-Types which is what gets passed through the janky AddParameter() overload you're using.

AddParameter(contentType中,内容ParameterType.RequestBody)从来就不是被直接调用。它被添加作为一种变通方法来传递,虽然从数据AddBody()但随后其他事情变得依赖于它,所以它仍然呆在那里。这是在事后一个可怕的决定,但为时已晚去改变它在1XX版本一致。如果我建一个版本,我会让这更明显。

AddParameter(contentType, content, ParameterType.RequestBody) was never meant to be called directly. It was added as a workaround to pass though data from AddBody() but then other things became dependent on it so it stuck around. It was a terrible decision in hindsight but it's too late to change it in the 1xx version line. If I ever build another version I'll make this more obvious.

这篇关于是否RestSharp覆盖手动设置Content-Type的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆