Shaun
Shaun

Reputation: 2181

Can't read raw POST body data - how to do this in this example?

I'm setting an API for my server for another developer. I'm currently using Flash AIR to send POST data to my server, and simply extract the variables as in

$command = $_POST['command'].

However, he's not using Flash, and is sending data like so:

https://www.mysite.com POST /api/account.php?command=login HTTP/1.1
Content-Type: application/json
Connection: close

command=login
params {"pass":"12345678","token":"","appID":"theirApp","user":"johnnyb","ver":"2.0","Library_ID":"1"}

My server is returning him an error saying that the 'command' parameter is missing.

What do I need to do my end to extract the $_POST var 'command' from his above data?

I've tried file_get_contents('php://input') and http_get_request_body(), but although they don't error, they don't show anything.

Thanks for your help.

Upvotes: 1

Views: 1959

Answers (3)

David M. Cotter
David M. Cotter

Reputation: 154

"Content-Type should be www-form-urlencoded" from @Cole (correct answer)

More info here: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1

Upvotes: 2

Quentin
Quentin

Reputation: 944035

The request claims that it is sending JSON.

Content-Type: application/json

However, this:

command=login
params {"pass":"12345678","token":"","appID":"theirApp","user":"johnnyb","ver":"2.0","Library_ID":"1"}

… is not JSON.

If you get rid of everything before the { then it would be JSON and you should be able to read it with file_get_contents('php://input') (and could then pass it through a decoder.

I've tried file_get_contents('php://input') and http_get_request_body() … they don't show anything.

They should work.

When I print out file_get_contents('php://input') for the comms … I get command=login, yet...

I thought you said you didn't get anything

if(!isset($_POST['command']))

$_POST will only be populated for the two standard HTML form encoding methods. If you are using JSON then it won't be automatically parsed, you have to do it yourself (with valid JSON input (so the additional data would need to be encoded in the JSON text with the rest of the data)), file_get_contents('php://input') and decode_json).

Upvotes: 2

Cole
Cole

Reputation: 1503

The command parameter needs to be part of the data, and the whole thing should be valid JSON. As is, command=login, it is not valid JSON.

Add it to the params object or make a containing object, like

{
 command:'login',
 params :{"pass":"12345678","token":"","appID":"theirApp","user":"johnnyb","ver":"2.0","Library_ID":"1"}
}

Upvotes: 1

Related Questions