Send POST data via raw json with postman

后端 未结 5 1982
误落风尘
误落风尘 2020-12-04 15:59

I\'ve got Postman (the one that doesn\'t open in Chrome) and I\'m trying to do a POST request using raw json.

In the Body tab I have \"raw\" selected and \"JSON (app

相关标签:
5条回答
  • 2020-12-04 16:31

    meda's answer is completely legit, but when I copied the code I got an error!

    Somewhere in the "php://input" there's an invalid character (maybe one of the quotes?).

    When I typed the "php://input" code manually, it worked. Took me a while to figure out!

    0 讨论(0)
  • 2020-12-04 16:35

    Install Postman native app, Chrome extension has been deprecated. (Mine was opening in own window but still ran as Chrome app)

    0 讨论(0)
  • 2020-12-04 16:38

    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 is a read-only stream that allows you to read raw data from the request body.

    $_POST is form variables, you will need to switch to form radiobutton in postman then use:

    foo=bar&foo2=bar2
    

    To post raw json with jquery:

    $.ajax({
        "url": "/rest/index.php",
        'data': JSON.stringify({foo:'bar'}),
        'type': 'POST',
        'contentType': 'application/json'
    });
    
    0 讨论(0)
  • 2020-12-04 16:46

    Just check JSON option from the drop down next to binary; when you click raw. This should do

    0 讨论(0)
  • 2020-12-04 16:46

    I was facing the same problem, following code worked for me:

    $params = (array) json_decode(file_get_contents('php://input'), TRUE);
    print_r($params);
    

    0 讨论(0)
提交回复
热议问题