使用Postman通过原始JSON发送POST数据 [英] Send POST data via raw JSONwith Postman
问题描述
我有Postman(在Chrome中无法打开的邮递员),我正在尝试使用原始JSON发出POST请求.
I've got Postman (the one that doesn't open in Chrome) and I'm trying to do a POST request using raw JSON.
在正文"标签中,我有原始"选择并"JSON(application/json)";有了这个身体:
In the Body tab I have "raw" selected and "JSON (application/json)" with this body:
{
"foo": "bar"
}
对于标题,我有1个, Content-Type:application/json
For the header I have 1, Content-Type: application/json
在PHP方面,我现在只是做 print_r($ _ POST);
,而我得到的是一个空数组.
On the PHP side I'm just doing print_r($_POST);
for now, and I'm getting an empty array.
如果我使用jQuery并这样做:
If I use jQuery and do:
$.ajax({
"type": "POST",
"url": "/rest/index.php",
"data": {
"foo": "bar"
}
}).done(function (d) {
console.log(d);
});
我得到了预期的结果:
Array
(
[foo] => bar
)
那为什么不与Postman合作呢?
So why isn't it working with Postman?
邮递员屏幕截图:
和标题:
推荐答案
与 jQuery
不同,为了读取原始的 JSON
,您需要在PHP中对其进行解码.>
Unlike jQuery
in order to read raw JSON
you will need to decode it in PHP.
print_r(json_decode(file_get_contents("php://input"), true));
php://input
是只读流,允许您从请求正文中读取原始数据.
php://input
is a read-only stream that allows you to read raw data from the request body.
$ _ POST
是表单变量,您需要在 postman
中切换到 form
单选按钮,然后使用:
$_POST
is form variables, you will need to switch to form
radiobutton in postman
then use:
foo=bar&foo2=bar2
要使用 jquery
发布原始的 json
:
$.ajax({
"url": "/rest/index.php",
'data': JSON.stringify({foo:'bar'}),
'type': 'POST',
'contentType': 'application/json'
});
这篇关于使用Postman通过原始JSON发送POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!