据我所知,有两个地方可以设置内容类型:
客户端为他要发送给服务器的正文设置一个内容类型(例如post) 服务器为响应设置内容类型。
这是否意味着我不必或不应该为所有的get请求(客户端)设置内容类型?如果我可以或者应该,那会是什么内容类型?
此外,我在一些帖子中读到,客户端的内容类型指定了客户端希望接收的内容类型。也许我的第一点是不对的?
据我所知,有两个地方可以设置内容类型:
客户端为他要发送给服务器的正文设置一个内容类型(例如post) 服务器为响应设置内容类型。
这是否意味着我不必或不应该为所有的get请求(客户端)设置内容类型?如果我可以或者应该,那会是什么内容类型?
此外,我在一些帖子中读到,客户端的内容类型指定了客户端希望接收的内容类型。也许我的第一点是不对的?
当前回答
我发现这篇文章在阐明“内容类型”概念方面非常有用,你可以查看它,在最后,有一个HTTP内容类型标头的常见值列表。
https://www.geeksforgeeks.org/http-headers-content-type/
其他回答
The problem with not passing over the content-type on a GET message is that sure the content-type is irrelevant because the server side determines the content anyway. The problem that I have encountered is that there are now a lot of places that set up their webservices to be smart enough to pick up the content-type that you pass and return the response in the 'type' that you request. Eg. we are currently messaging with a place that defaults to JSON, however, they have set their webservice up so that if you pass a content-type of xml they will then return xml rather than their JSON default. Which I think going forward is a great idea
Get请求不应该有内容类型,因为它们没有请求实体(即主体)。
GET请求可以有“Accept”报头,表示客户机可以理解哪些类型的内容。然后服务器可以使用它来决定发送回哪种内容类型。
不过它们是可选的。
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
公认的答案是错误的。引用是正确的,PUT和POST必须拥有它的断言是不正确的。没有要求PUT或POST实际上有额外的内容。也没有禁止GET实际拥有内容。
rfc确切地表达了他们的意思。如果你方(客户端或源服务器)将发送额外的内容,除了HTTP报头,它应该指定一个内容类型报头。但是请注意,可以省略content - type而仍然包含内容(例如,通过使用content - length标头)。
我发现这篇文章在阐明“内容类型”概念方面非常有用,你可以查看它,在最后,有一个HTTP内容类型标头的常见值列表。
https://www.geeksforgeeks.org/http-headers-content-type/