使用邮递员通过原始json发送POST数据 [英] Send POST data via raw json with postman

查看:92
本文介绍了使用邮递员通过原始json发送POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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

在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'
});

这篇关于使用邮递员通过原始json发送POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆