POST的Content-Type引发的HTTP相关知识思考

xiaoxiao2021-02-28  10

关于Content-Type 语法例子 POST常见数据提交类型 applicationx-www-form-urlencodedmultipartform-dataapplicationjsontextxmlbinary applicationoctet-stream 关于HTTP HTTPHTTP请求方法HTTP响应状态码HTTP的头域 通用头域请求头域响应头域实体头域 关于MIME类型 语法独立类型 Discrete types多部分类型 Multipart typesMIME嗅探

参考链接: Content-Type in MDN web docs Form content type in W3C Recommendation

关于Content-Type

Content-Type是实体头域(或称为实体头部,entity header)用于向接收方指示实体(entity body)的介质类型的,或称为资源的MIME类型,现在通常称media type更为合适。(例如,指定HEAD方法送到接收方的实体介质类型,或GET方法发送的请求介质类型,表示后面的文档属于什么MIME类型。)

在响应中,Content-Type标头告诉客户端实际返回的内容的内容类型。浏览器会在某些情况下进行MIME嗅探,并不一定遵循此标题的值; 为了防止这种行为,可以将标题 X-Content-Type-Options 设置为 nosniff。

在请求中 (如POST 或 PUT),客户端告诉服务器实际发送的数据类型。

语法

Content-Type: text/html; charset=utf-8 Content-Type: multipart/form-data; boundary=something[1 to 70 characters]

media-type 资源或数据的 media type charset 字符编码标准 boundary 对于多部分(multipart)实体,boundary 是必需的,它用于封装消息的多个部分的边界。其由1到70个字符组成,浏览器自动生成,该字符集对于通过网关鲁棒性良好,不以空白结尾。

例子

Content-Type 在HTML表单中 在通过HTML form提交生成的POST请求中,请求头的Content-Type由元素上的enctype属性指定:

<form action="/" method="post" enctype="multipart/form-data"> <input type="text" name="description" value="some text"> <input type="file" name="myFile"> <button type="submit">Submit</button> </form>

请求头看起来像这样(在这里省略了一些 headers):

POST /foo HTTP/1.1 Content-Length: 68137 Content-Type: multipart/form-data; boundary=---------------------------974767299852498929531610575 ---------------------------974767299852498929531610575 Content-Disposition: form-data; name="description" some text ---------------------------974767299852498929531610575 Content-Disposition: form-data; name="myFile"; filename="foo.txt" Content-Type: text/plain (content of the uploaded file foo.txt) ---------------------------974767299852498929531610575

POST常见数据提交类型

HTTP中,提交数据的方式,最常用的就是GET和POST。 GET方式,是把参数按键值对通过QueryString的方式放在URL尾部,比如: http://www.example.com/test.html?a=1&b=2 POST方法,通常是把要提交的表单放在一个Form中,指明action后就可以提交数据 其实这些都是表象,W3C上对如何处理表单有明确的过程说明:

When the user submits a form (e.g., by activating a submit button), the user agent processes it as follows. Step one: Identify the successful controls

Step two: Build a form data set

Step three: Encode the form data set The form data set is then encoded according to the content type specified by the enctype attribute of the FORM element.

Step four: Submit the encoded form data set Finally, the encoded data is sent to the processing agent designated by the action attribute using the protocol specified by the method attribute.

This specification does not specify all valid submission methods or content types that may be used with forms. However, HTML 4 user agents must support the established conventions in the following cases:

If the method is “get” and the action is an HTTP URI, the user agent takes the value of action, appends a `?’ to it, then appends the form data set, encoded using the “application/x-www-form-urlencoded” content type. The user agent then traverses the link to this URI. In this scenario, form data are restricted to ASCII codes.If the method is “post” and the action is an HTTP URI, the user agent conducts an HTTP “post” transaction using the value of the action attribute and a message created according to the content type specified by the enctype attribute.

翻译过来就是告诉我们,提交数据时需要通过表单enctype属性(规定在发送到服务器之前应该如何对表单数据进行编码)根据content type进行编码。并且,如果是GET,用”?”连接,编码方式为“application/x-www-form-urlencoded”;如果是POST则根据enctype属性确定content type,默认也为”application/x-www-form-urlencoded”。

附enctype取值编码含义

值描述application/x-www-form-urlencoded在发送前编码所有字符(默认,空格转换为 “+” 加号,特殊符号转换为 ASCII HEX 值)multipart/form-data不对字符编码(在使用包含文件上传控件的表单时,必须使用该值)text/plain纯文本(空格转换为 “+” 加号,但不对特殊字符编码,据说get方式会这样,post时不会)

因此,POST请求的消息主体放在entity body中,服务端根据请求头中的Content-Type字段来获取消息主体的编码方式,进而进行解析数据。

application/x-www-form-urlencoded

最常见的 POST 提交数据的方式,原生Form表单,如果不设置 enctype 属性,默认为application/x-www-form-urlencoded 方式提交数据。请求类似于下面这样(无关的请求头域已略去):

POST http://www.example.com HTTP/1.1 Content-Type: application/x-www-form-urlencoded;charset=utf-8 name=test&val1=1&val2=
转载请注明原文地址: https://www.6miu.com/read-1250082.html

最新回复(0)