简体   繁体   English

是否有内容的内容类型标头,就像对Web api的PUT / POST请求中的请求标头一样

[英]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.

相关问题 C# 带有自定义标头的 HttpClient POST 请求发送不正确的 Content-Type header 字段 - C# HttpClient POST Request with Custom Headers sends incorrect Content-Type header field 验证请求内容类型 - Validating Request Content-Type .NET Core WebAPI / Angular 项目 - 请求 header 字段内容类型在预检响应中不允许访问控制允许标头 - .NET Core WebAPI / Angular project - Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response POST请求标头中的content-type缺少FormData边界 - FormData boundary missing from content-type in POST request header 如何将Content-Type标头添加到.Net GET Web请求? - How to add the Content-Type header to a .Net GET web request? 如何使用Content-Type:multipart / form-data通过Web API请求主体参数访问? - How request body parameter access by Web API using Content-Type: multipart/form-data? 在.NET MVC 4(Web API)中,如何拦截控制器请求并更改其内容类型? - In .NET MVC 4 (Web API), how do I intercept a request for a controller and change it's Content-Type? ASP.NET 核心 jQuery POST - 尽管标题格式正确,但内容类型不正确 - ASP.NET Core jQuery POST - Incorrect Content-Type despite correctly formatted headers 在WCF ReSTful服务中,POST或PUT请求的Content-Type是否需要为“ application / x-www-form-urlencoded”? - Does the Content-Type of a POST or PUT request need to be 'application/x-www-form-urlencoded' in a WCF ReSTful service? restsharp PUT 请求错误-->“StatusCode: InternalServerError, Content-Type: application/json, Content-Length: -1)” - restsharp PUT request error --> "StatusCode: InternalServerError, Content-Type: application/json, Content-Length: -1)"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM