表单数据与查询字符串

Mah*_*Ali 4 http form-data

我试图了解浏览器如何解释表单数据。我知道http请求由[Method][Header][URL][Params][Body]组成

我不知道如何在那里适应表单数据?它被解释为参数(查询字符串)还是在正文中发送?标头中的 application/x-www-form-urlencoded 是什么?

Exp*_*ity 5

表单数据确实是在 POST 请求的 HTTP 主体内发送的

如果我拆解 POST 请求:

Request line >  POST /index.php HTTP/1.1

Headers      >  Cache-Control: max-age=0
             >  Origin: http://localhost:8080
             >  Upgrade-Insecure-Requests: 1
             >  DNT: 1

The content
type header  >  Content-Type: application/x-www-form-urlencoded

Also headers >  User-Agent: Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/85.0.4183.121Safari/537.36
             >  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
             >  Sec-Fetch-Site: same-origin
             >  Sec-Fetch-Mode: navigate
             >  Sec-Fetch-User: ?1
             >  Sec-Fetch-Dest: document
             >  Referer: http://localhost:8080/index.php
             >  Accept-Encoding: gzip,deflate,br
             >  Accept-Language: cs-CZ,cs;q=0.9,en;q=0.8

Empty line   >

Body         >  fname=John&lname=Doe&formsubmitted=5000
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,在其他混乱的标头中(对此我深表歉意),有一个Content-Type标头,它指定 HTTP Body 中内容的 MIME 类型,这通常application/x-www-form-urlencoded用于 POST 表单,这就是您在上面的 HTTP 请求。

这里,Body 包含&来自以下表单的表单数据(用 分隔):

Request line >  POST /index.php HTTP/1.1

Headers      >  Cache-Control: max-age=0
             >  Origin: http://localhost:8080
             >  Upgrade-Insecure-Requests: 1
             >  DNT: 1

The content
type header  >  Content-Type: application/x-www-form-urlencoded

Also headers >  User-Agent: Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/85.0.4183.121Safari/537.36
             >  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
             >  Sec-Fetch-Site: same-origin
             >  Sec-Fetch-Mode: navigate
             >  Sec-Fetch-User: ?1
             >  Sec-Fetch-Dest: document
             >  Referer: http://localhost:8080/index.php
             >  Accept-Encoding: gzip,deflate,br
             >  Accept-Language: cs-CZ,cs;q=0.9,en;q=0.8

Empty line   >

Body         >  fname=John&lname=Doe&formsubmitted=5000
Run Code Online (Sandbox Code Playgroud)

使用的另一个是multipart/form-data,引用了developer.mozzila.com上的MIME类型参考:“作为一种多部分文档格式,它由不同的部分组成,由边界分隔(以双破折号--开头的字符串)。每个部分都是它自己的实体,具有自己的 HTTP 标头、Content-Disposition 和用于文件上传字段的 Content-Type。”,此定义还包括以下示例:

Content-Type: multipart/form-data; boundary=aBoundaryString
(other headers associated with the multipart document as a whole)

--aBoundaryString
Content-Disposition: form-data; name="myFile"; filename="img.jpg"
Content-Type: image/jpeg

(data)
--aBoundaryString
Content-Disposition: form-data; name="myField"

(data)
--aBoundaryString
(more subparts)
--aBoundaryString--
Run Code Online (Sandbox Code Playgroud)

虽然我从未真正遇到过multipart/form-dataMIME 类型,但在处理 POST HTTP 请求时必须承认它

为了更详细地回答您的其他问题“标头中的 application/x-www-form-urlencoded 是什么?”
application/x-www-form-urlencoded是一个 MIME 类型,其中的 urlencoded 表示表单数据的编码方式与 GET 请求中的编码方式相同,每个字段由一个&字符分隔,字段的格式为name=value

对于您所写的“我知道http请求由[Method][Header][URL][Params][Body]”组成,这是错误的,HTTP请求由这些组成,但不是按这个顺序,实际的顺序这是:

Method Requested_Resource(and GET parameters if any) HTTP_Version \r\n
Headers ('\r\n' after each header)
\r\n
Body
Run Code Online (Sandbox Code Playgroud)