在表单提交数据时,需要告诉服务端自己的content-type,好让服务端处理。

  默认表单提交是x-www-form-urlencoded,还有一种常见的 multipart/form-data。那这俩的区别是啥呢?

  在postman工具中,很容易看出来二者的区别。

x-www-form-urlencoded:

  

查看http请求:

POST /hi.php HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: a1054dd4-5f58-038c--326721c85a7a id=%2B1&val=

在 x-www-form-urlencoded中,body体中是key=value&key=xx形式,并且是urlencode后的。

multipart/form-data:

查看http请求:

POST /hi.php HTTP/1.1
Host: localhost
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: dddb1f15-348b-5e3f-244c-aa0d8680bd3d ------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="id" +
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="val" ------WebKitFormBoundary7MA4YWxkTrZu0gW--

form-data中含有boundary=----***

常见的content-type还有一种json形式:

Content-Type: application/json

查看http请求:

POST /hi.php HTTP/1.1
Host: localhost
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 38f8844e-bd9f-a892-5107-a3f4e6b81a41 {"a":"xx"}

再看下$_POST和php://input的区别联系

利用一段测试代码:

<?php
$arr = $_POST;
$raw_post_data = file_get_contents('php://input', 'r'); file_put_contents('hsd.log', "\$_POST\r\n".print_r($arr,true)."\r\n", FILE_APPEND);
file_put_contents('hsd.log', "php://input\r\n".print_r($raw_post_data,true)."\r\n", FILE_APPEND);
file_put_contents('hsd.log', "content_type: ".print_r($_SERVER['HTTP_CONTENT_TYPE'],true)."\r\n", FILE_APPEND);
file_put_contents('hsd.log', print_r("====================",true)."\r\n", FILE_APPEND);

在几种content-type表现如下:(php version 5.5.3)

1. 当是x-www-form-urlencoded时,

$_POST
Array
(
[id] => 1+1
[val] => 44753
) php://input
id=1%2B1&val=44753
content_type: application/x-www-form-urlencoded

php://input中的形式和body体中是一样的,同样是urlencode过的。

2. 当是 multipart/form-data时,

$_POST
Array
(
[id] => 1+111
[val] => 44753
) php://input content_type: multipart/form-data; boundary=----WebKitFormBoundaryUUacTtaG65hX7g6y

发现$_POST依然可以获取到参数。此时php://input 获取不到值。

(测试在hhvm HipHop VM 3.0.1;baidu version:1.0.6.4 php://input可以获取到multipart/form-data 提交参数)如下:

$_POST
Array
(
[id] => 1+111
[val] => 44753
) php://input
------WebKitFormBoundaryxHArWQUlukzFfZAb
Content-Disposition: form-data; name="id" 1+111
------WebKitFormBoundaryxHArWQUlukzFfZAb
Content-Disposition: form-data; name="val" 44753
------WebKitFormBoundaryxHArWQUlukzFfZAb-- content_type: multipart/form-data; boundary=----WebKitFormBoundaryxHArWQUlukzFfZAb

3. 当是application/json时,

$_POST
Array
(
) php://input
{"a":"xx"}
content_type: application/json

此时,$_POST获取不到提交参数。php://input 可以获取到。这种json提交数据,php接收参数需要使用 file_get_contents('php://input', 'r')。

总结:

$_POST只能获取到form提交的数据,而php://input获取不到  multipart/form-data提交的数据。

这里顺便提下,如何提交给php获取$_POST的key=>value是数组形式。

$arrParams = array(
'person[0][first_name]' => 1,
'person[0][last_name]' => 1,
'person[1][first_name]' => 111,
'person[1][last_name]' => 222,
);

这样$_POST['person'] 就是个二维数组了。

$_POST
Array
(
[person] => Array
(
[0] => Array
(
[first_name] => a
[last_name] => b
) [1] => Array
(
[last_name] => d
) ) ) php://input
person%5B0%5D%5Bfirst_name%5D=a&person%5B0%5D%5Blast_name%5D=b&person%5B1%5D%5Blast_name%5D=c&person%5B1%5D%5Blast_name%5D=d
content_type: application/x-www-form-urlencoded

  

几种content-type提交以及$_POST 和php://input的更多相关文章

  1. 转载 SharePoint【Site Definition 系列】– 创建Content Type

    转载原地址:  http://www.cnblogs.com/wsdj-ITtech/archive/2012/09/01/2470274.html Sharepoint本身就是一个丰富的大容器,里面 ...

  2. the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header

    the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header ...

  3. php表单提交 图片、音乐、视频、文字,四种类型共同提交到数据库

    这个问题一直困扰了我好几天,终于在今天让我给解决了,难以掩饰的激动. 其实在之前没有接触到这种问题,只是表单提交数据而已,再就是图片,四种类型同时提交还真是没遇到过,做了一个系统,其中有一个功能就是提 ...

  4. 今天在研究jquery用ajax提交form表单中得数据时,学习到了一种新的提交方式

    今天在研究jquery用ajax提交form表单中得数据时,学习到了一种新的提交方式 jquery中的serialize() 方法 该方法通过序列化表单值,创建 URL 编码文本字符串 序列化的值可在 ...

  5. 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 ...

  6. .NET获取文件的MIME类型(Content Type)

    第一种:这种获取MIME类型(Content Type)的方法需要在.NET 4.5之后才能够支持,但是非常简单. 优点:方便快捷 缺点:只能在.NET 4.5之后使用 public FileResu ...

  7. org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported或其他Content type不支持处理

    很久没从头到尾搭框架,今天搭的过程中,springmvc controller方法入参用@RequestBody自动绑定参数时一直提示各种 not supported 排查问题有两个解决路径: 1)使 ...

  8. 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 ...

  9. Java之POI读取Excel的Package should contain a content type part [M1.13]] with root cause异常问题解决

    Java之POI读取Excel的Package should contain a content type part [M1.13]] with root cause异常问题解决 引言: 在Java中 ...

随机推荐

  1. Chrome Inspect不显示Webview页面的问题总结

    首先,确保手机打开了USB调试.如果还是检测不到WebView页面,主要有以下几种情况. 1.反应慢,稍等一会 2.关闭然后重新打开USB调试开关,刺激一下chrome,我的魅族手机有时需要这样操作一 ...

  2. IE打开https网站时,取消证书问题提示

    上面介绍了,调用IE来打开对应的网页问题,但是在实际测试中,有些网站是采用https协议的,这时候IE浏览器会弹出如下窗口,一般手动选择后,才可进入登录界面,那么该如何解决呢? 1.点击[继续浏览此网 ...

  3. JAVA设计模式——代理(静态代理)

    定义 为其它的对象提供一种代理,以控制这个对象的访问 使用场景 当不想直接访问某个对象的时候,就可以通过代理 1.不想买午餐,同事帮忙带 2.买车不用去厂里,去4s店 3.去代理点买火车票,不用去车站 ...

  4. C#面向对象 1

    using System; using System.Collections.Generic; using System.Collections; using System.Linq; using S ...

  5. ueditor在线编辑器的简单使用-上传图片

    由于我的项目个人博客网站需要用到在线编辑器,百度的ueditor编辑器就是一个很好的编辑器.开始比较迷茫的使用,各种百度,没有我满意的答案,明明可以很简单的使用. 1.首先进入ueditor官网下载, ...

  6. spring mvc 中 controller 路径配置

    下图中,由于红色部分(value="/")的存在,导致 host:port/项目/dimlist 无法被映射到dimList方法,解决办法是将其去掉. package cn.bgo ...

  7. c/c++ 网络编程与多线程 编译参数

    网络编程与多线程 编译参数 编译时要链接操作系统的pthread库 g++ -g socket01.cpp -std=c++11 -pthread 不加-pthread的话,出现下面的错误: term ...

  8. fedora 28/29 配置 C++ 环境

    最近 使用C++ 开发 更换机器的时候,还要重新配置一下 gnu 工具链.于是简单进行了安装了一下: yum install gcc yum install gcc-c++ yum install g ...

  9. 设置Linux环境变量的方法和区别_Ubuntu

    设置 Linux 环境变量可以通过 export 实现,也可以通过修改几个文件来实现,有必要弄清楚这两种方法以及这几个文件的区别. 通过文件设置 Linux 环境变量 首先是设置全局环境变量,对所有用 ...

  10. SD寄售-寄售补充

    SD寄售-寄售补充 概述: 寄售就是企业将货物寄放在客户处,或者在自己的仓库为客户保留库存,货物所有权归公司,当客户将库存消耗掉,才算公司的销售.公司才开票给客户. SAP SD寄售涉及4个业务过程, ...