[英]Is it necessary to have content-type headers for content much like request headers in a PUT/POST request to web api
I'm new to web api and writing a code where by I'm sending in json data for a PUT/POST request to web api (web service). 我是Web api的新手,并编写代码,在其中我将JSON数据发送到Web api(Web服务)的PUT / POST请求。 I'm doing the following
我正在做以下
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:9000/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var gizmo = some json data;
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post,"url");
req.Content = new StringContent(some json data, Encoding.UTF8,"application/json"));
client.Timeout = TimeSpan.FromSeconds(500);
response = await client.PostAsJsonAsync("api/products", gizmo);
}
My question is do I essentially have to put the code for content-type for content header or not and I have observed that even if do include content-type as "applicatipn/json" exclusively for content-type in my code and check my request in Fiddler, it still shows content-type :text/html. 我的问题是,我是否基本上必须将内容类型的代码放置在内容标头中,还是已经观察到,即使在我的代码中确实包含了内容类型作为“ applicatipn / json”,仅用于内容类型并检查我的请求在Fiddler中,它仍然显示content-type:text / html。 Why is that?
这是为什么? .
。 All your replies will be highly appreciated
您的所有回复将受到高度赞赏
Yes, you do put code for content-type. 是的,您确实为内容类型放置了代码。 The bit you don't have to include with JSON is the accepts, although I personally feel it is better to include accepts, as it explicitly states intent, but your mileage may vary.
您不必在JSON中包含的一点是接受,尽管我个人认为最好包含接受,因为它明确指出了意图,但是您的工作量可能会有所不同。
Here is a blog post I just found that explains some of the issues around not doing this: http://truncatedcodr.wordpress.com/2012/09/05/asp-net-web-api-always-set-content-type/ 这是我刚刚发现的博客文章,它解释了有关不执行此操作的一些问题: http : //truncatedcodr.wordpress.com/2012/09/05/asp-net-web-api-always-set-content-type /
EDIT: 编辑:
See comments. 看评论。 Apparently, you do not have to specify content type any more.
显然,您不必再指定内容类型。 Regardless, explicit coding is preferred to implicit coding, at least in most Enterprise software.
无论如何,至少在大多数企业软件中,显式编码比隐式编码更可取。 The reason is simple: When you use implicit coding, the intent of the code is often lost.
原因很简单:使用隐式编码时,代码的意图经常丢失。 Note that I am not saying "don't use abstractions that are available", as there is nothing wrong with allowing Microsoft (or an open source development team) to take over the plumbing of your application and use the abstractions they provide to simplify and reduce your code.
请注意,我并不是说“不要使用可用的抽象”,因为允许Microsoft(或开放源代码开发团队)接管您的应用程序并使用它们提供的抽象来简化和改进没有错。减少您的代码。 If you rely on defaults, rather than set the values, you create some future risk, and this should be considered.
如果您依赖默认值,而不是设置值,则可能会带来一些未来风险,因此应予以考虑。 In some cases, it is worth the risk.
在某些情况下,值得冒险。 The tendency, today, is to use implicit coding as much as possible, as it saves keystrokes.
今天的趋势是尽可能使用隐式编码,因为它可以节省击键次数。 As a consultant, I can attest this is very often the core of future problems.
作为顾问,我可以证明这通常是未来问题的核心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.