Hướng dẫn file_get_contents php input postman

this is my code. and i am posting my data using post method.

$post_body = file_get_contents('php://input');
if(getenv('REQUEST_METHOD') == 'POST'){
    print_r( array( "3","3d"));
}
print_r($post_body);

i am using chrome's postman to test this code,but it don't print post data, see output image.

Hướng dẫn file_get_contents php input postman

what i am doing wrong???

EDIT

To show how i solve my problem i am adding this image.

Hướng dẫn file_get_contents php input postman

asked May 4, 2016 at 6:11

ArchishArchish

8207 silver badges30 bronze badges

According to PHP documentation

php://input is not available with enctype="multipart/form-data".

Postman supports different types of Content-Type including multipart/form-data which seems to be default.

So make sure you don't use multipart/form-data(in the Body tab).

answered May 4, 2016 at 6:23

Ruslan OsmanovRuslan Osmanov

19.7k7 gold badges44 silver badges57 bronze badges

4

I had similar trouble testing symfony endpoint but with completely different cause:

My class had only functions in it. And i did a

var_dump(file_get_contents('php://input'));

on the first line of my function, which happens to be the first function in the class. This was returning empty("") even if parameters were correct.

My problem was that i forgot to use the FULL route to this, but since postman tried to run this function anyway got me too much time to notice. Hope it helps!

answered Sep 18, 2019 at 9:59

Hướng dẫn file_get_contents php input postman

MbotetMbotet

922 silver badges17 bronze badges

@harryi3t I don't think so because I've had this issue with Postman for ages. I have never been able to get one of these requests to work in Postman. You could be right, but then why are so many people having issues with this? Are there limitations or rules to using the raw json posting features that we aren't aware of?

I've created a test page using AJAX to post the request I'd like to make to my server and that works perfectly fine, but the moment I try it in Postman it doesn't.
It's as if Postman clears the post data before making the request to the server because if I dump the request data on the server side I get nothing back if I make the request through Postman!

Out of interest this is the script I'm using to send the AJAX request:

const $requestForm = document.getElementById('requestForm');
const $requestJson = document.getElementById('requestJson');

$requestForm.addEventListener('submit', function(e) {
    e.preventDefault();

    const xhr = new XMLHttpRequest();

    xhr.open('POST', $requestForm.action, true);
    xhr.setRequestHeader('Content-Type', 'application/json')
    xhr.onload = function() {
        console.dir(xhr);
    };
    xhr.send(JSON.stringify($requestJson.value));
});

And the server simply dies with the request body:

die(file_get_contents('php://input'));

Works using the AJAX script, doesn't using Postman.