概述

在POST请求中,理论上请求端程序可以发送任意格式报文正文,但是最好在报文头Content-Type字段标明正文的格式,方便接收端根据Content-Type正确处理正文。

传统HTML-form表单提交

html-form表单中,post正文格式被定义在enctype属性中,该属性一共定义了3个值,分别对应Content-Type的3个值:

enctype值 enctype描述 对应Content-Type的值 正文段格式示例
application/x-www-form-urlencoded 在发送前编码所有字符,http_query编码方式(默认) application/x-www-form-urlencoded manid=qwert&file-a=1ca6f20fd0c29742bec9914423140d03.jpg
multipart/form-data 不对字符编码,正文内容是每一个表单字段进行boundary分隔组成的报文,支持二进制数据提交 multipart/form-data; boundary=XXXXXXXXXX(请求端程序自行生成的boundary) ------WebKitFormBoundaryNOdBuEzJYqyffk3l
Content-Disposition: form-data; name="manid"

qwert
------WebKitFormBoundaryNOdBuEzJYqyffk3l
Content-Disposition: form-data; name="file-a"; filename="1ca6f20fd0c29742bec9914423140d03.jpg"
Content-Type: image/jpeg

#¥%%……&%F……%

------WebKitFormBoundaryNOdBuEzJYqyffk3l--

text/plain 空格转换为 "+" 加号,但不对特殊字符编码 text/plain manid=qwert
file-a=1ca6f20fd0c29742bec9914423140d03.jpg

只有enctype="multipart/form-data"能够提交form input type="file"字段选择的文件,其余两种编码方式只能够提交文件名。

自定义提交

发送端只需要遵循HTTP报文规范,就可以自行自定义发送post报文,这跟是否带有Content-Type无关,但是接收端也要自定义解析规则,如果是通用的解析规则,通常只能支持规范中的Content-Type类型解析。

PHP

PHP自带的报文解析只支持html form enctype原生的前2种解析方式("application/x-www-form-urlencoded","multipart/form-data"),通常字符类型的字段解析到全局变量$_POST中,二进制内容解析到全局变量$_FILES中。

也可以运用自带封装协议"php://input"读取请求正文,但是当enctype="multipart/form-data"的情况下,这种方式是无效的,其余情况,包括自定义的Content-Type值,都可以读取。

[HTTP]POST报文中Content-Type对正文解析的影响的更多相关文章

  1. springboot 报错 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

    开始 controller 方法写的是 @RequestMapping( value = "/add", method = RequestMethod.POST ) public ...

  2. jmeter报"msg":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported"的解决方法

    1.报"msg":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supporte ...

  3. Jmeter发送post请求报错Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

    常识普及: Content-type,在Request Headers里面,告诉服务器,我们发送的请求信息格式,在JMeter中,信息头存储在信息头管理器中,所以在做接口测试的时候,我们维护Conte ...

  4. xml文件报Element 'beans' cannot have character [children],because the type's content type is element

    写springMvc.xml文件时,偶然遇到 Element 'beans' cannot have character [children],because the type's content t ...

  5. 遇到问题之“postman报Unsupported Media Type: Content type 'text/plain;charset=UTF-8' not supported”

    postman报Unsupported Media Type: Content type 'text/plain;charset=UTF-8' not supported postman之所以报Uns ...

  6. Jsoup问题---获取http协议请求失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.

    Jsoup问题---获取http协议请求失败 1.问题:用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不 ...

  7. Jsoup获取部分页面数据失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.

    用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...

  8. Springs Element 'beans' cannot have character [children], because the type's content type is element-only

    Springs Element 'beans' cannot have character [children], because the type's content type is element ...

  9. 响应http报文中的Date属性与cookie过期时间的关系

    今天在測试.net时,发现一个莫名其妙的问题:cookie老是保存不到浏览器端; 经过细致的比对成功与不成功的报文,居然无意中发现好像Date与它有关系,这太让我意想不到了,从来不知道cookie保存 ...

随机推荐

  1. python多线程场景下print丢失

    python多线程情况下,print输出会出现丢失的情况,而logging模块的日志输出不会. 以下是示例代码,多运行几次就会发现这个有意思的现象 # coding:utf-8 import thre ...

  2. vue中怎么全局引入sass文件

    1.添加依赖 npm install sass-resources-loader --save-dev 2.修改build/utils.js scss: generateLoaders('sass') ...

  3. xml 解析参考文档

    https://www.cnblogs.com/a1656344531/archive/2012/11/28/2792863.html

  4. javaWeb2之Jsp

    Java Server Page 放在除WEB-INF(放web项目的隐私文件)外的其他任何目录中. Java服务器端网页,实现在HTML页面编写Java代码实现动态内容,一种简化Servlet编写的 ...

  5. qt 访问容器

    #include <iostream> #include <QString> #include <QList> #include <QListIterator ...

  6. Xilinx FPGA 移位寄存器IP延时问题

    软件版本:Vivado2016.1 在使用移位寄存器IP时,对于不同延时拍数的使能延时可能会有问题. (1)32深度的可变长度移位寄存器,IP生成界面如下图所示. (2)128深度的可变长度移位寄存器 ...

  7. 百度编辑器前后端二开图片上传Js Thinkphp tp5 ueditor

    百度编辑器图片上传Jsueditor.all.min.js 下载链接 链接:https://pan.baidu.com/s/1VNgw9ELgRRHKeCQheFkQTw 提取码:fnfi 使用方法: ...

  8. UOJ#449. 【集训队作业2018】喂鸽子(期望dp)

    题意 有 \(n\) 只鸽子,每只鸽子需要 \(k\) 粒玉米才能喂饱.问每次随意喂给 \(n\) 个鸽子中的一个,期望多久所有鸽子都被喂饱. 对于 \(998244353\) 取模. 数据范围 \( ...

  9. foreach 中获取索引index的方法

    一样,很少用到,记下来先 主要代码: foreach (var item in arr) { int index = arr.indexOf(item); //index 为索引值 }

  10. nuxt npm run dev 报错Solution to the "Error: listen EADDRINUSE 127.0.0.1:8080"

    Solution to the "Error: listen EADDRINUSE 127.0.0.1:8080" Hello, Just sharing a solution t ...