问题
利用postman模拟客户端发请求,但是需要客户端发的json数据需要嵌套好几层,如果使用form-data或者x-www-form-urlencode不方便写请求参数,所以用了raw形式,直接传json字符串,但是问题出现了后台接受不到数据
解决
- x-www-form-urlencode
后来在网上搜索说,php后台默认接受x-www-form-urlencode 这个形式的。


再后台打印出来http请求的header信息

当选择x-www-form-urlencode形式时,发送数据时,不管你在请求头设置content-type是什么格式,实际发送到后台时,请求头为x-www-form-urlencode这种形式。在发送时数据被编码为名称/值对。存到$_POST数组中。
- form-data
当选择form-data形式时,发送数据时,不管你在请求头设置content-type是什么格式,实际发送到后台时,请求头为multipart/from-data这种形式。在发送时数据被编码为名称/值对。存到$_POST数组中。一般上传文件才会使用这种方式
- RAW
当选择Raw时发送json字符串时,http请求头content-type为text/plain形式,需要
$jsonstr = file_get_contents('php://input');
$jsonobj = json_decode($jsonstr,true);
这个方法接受,然后再进行使用
网友评论