【问题标题】:formData vs query stringformData 与查询字符串
【发布时间】:2021-01-14 07:13:40
【问题描述】:

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

组成

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

【问题讨论】:

    标签: http form-data


    【解决方案1】:

    表单数据确实是在 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
    

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

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

    <form action="/index.php" method="POST">
        <input type="text" id="fname" name="fname" value=""><br>
        <input type="text" id="lname" name="lname" value=""><br>
        <input type="hidden" id="formsubmitted" name="formsubmitted" value="5000"><br>
        <input type="submit" value="Submit">
    </form>
    

    另一个使用的是multipart/form-data,引用MIME type reference on developer.mozzila.com:“作为多部分文档格式,它由不同的部分组成,由边界分隔(以双破折号--开头的字符串)。每个部分是它自己的实体,具有自己的 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--
    

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

    更详细地回答您的其他问题“标题中的 application/x-www-form-urlencoded 是什么?”
    application/x-www-form-urlencoded 是一种 MIME 类型,其中的 urlencoded 表示表单数据的编码方式与 GET 请求中相同,每个字段由 &amp; 字符分隔,字段位于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
    

    【讨论】:

      【解决方案2】:

      表单数据确实是在正文中发送的。

      application/x-www-form-urlencoded 是表单的格式。这是在Content-Type 标头中设置的。

      另一种格式是multipart/form-data,通常在表单有文件上传字段时使用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-16
        • 2021-04-22
        • 2013-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-24
        • 2010-10-06
        相关资源
        最近更新 更多