使用 Postman 通过原始 JSON 发送 POST 数据 [英] Send POST data via raw JSONwith Postman

查看:30
本文介绍了使用 Postman 通过原始 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(应用程序/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屋!

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