I'm using a firefox pluing called restclient to simulate a post request. It doesnt' seem to be picking up any post data, but not sure if I'm formatting it properly.
using header: Content-Type: application/json
and body: {"id":1234}
but not go, it's not picking up the id parameter in my php, is there some special formatting I need to set?
okay, got it working, here is what is needed
two content types:
Content-Type: application/json
Content-Type: application/x-www-form-urlencoded
and then set your params like so in body:
param1=value1¶m2=value2
Thanks for the help everyone.
PHP will not parse a JSON body automatically into the $_POST superglobal. That only happens with application/x-www-form-urlencoded and multipart/form-data POST bodies. That said, you can parse the body yourself — you can access the raw POST body via the php://input pseudo-stream.
Related
i know that when a simple form post from client to server by post method, the sending request has this header:
Content-Type: application/x-www-form-urlencoded
now, when the server return an answer to the request, is it posible to create the above header as Response header?
(My server language is PHP)
Works fine for:
Sending from Android platform to PHP (web service)
Request headers sent with this Content-Type: application/x-www-form-urlencoded
Not working for:
Request headers sent with Content-Type: application/json. no data is received
API is working on same platform but not in cross platform:
Web to web WORKING
Android to web NOT WORKING
In PHP added both header on top:
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
If you compare these two content types application/x-www-form-urlencoded and application/json in a "The first works with PHP, the second doesn't", then you probably expect the data to appear magically inside $_POST in both cases.
This will not happen. PHP only fills $_POST if the first content type is given (alternatively, application/multipart-form-data can be used for everything, especially file uploads).
If you want to use application/json, then you have to implement a parser on the PHP side yourself that reads the HTTP request body and parses it to your liking.
after log search i found my answer..
we need this function to get $_POST response from cross platform (android to web)
file_get_contents('php://input')
OR we can also use this function to get $_POST response
$HTTP_RAW_POST_DATA
here is complete function to get the response.
$data = urldecode(file_get_contents('php://input'));
echo $data;
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.
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).
"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
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"}
}
Say you submitted the form and you have in the Request Body:
data[first]=johnny&data[last]=appleseed
The normal POST request works fine from the web, but when submitted from Fiddler the PHP complains that 'data' is not an array.
I'm not sure how to format the request body such that 'data' gets recognized as an array.
Yes I'm using Content-Type: application/x-www-form-urlencoded.
Thanks
Found the problem, wasn't encoding the request body properly (i.e. wasn't using %5B for '[' and %5D for ']')
Hi i am having problems with an XHR post request.
in javascript:
self.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
self.xhr.setRequestHeader("Method", "POST " + url + " HTTP/1.1");
In firebug:
Parametersapplication/x-www-form-urlencoded
{"u":"andrepadez","m":"\n...
JSON
m
"sfdsfsdfdsfdsf"
u
"andrepadez"
Source
{"u":"andrepadez","m":"\nsfdsfsdfdsfdsf"}
In .NET, I post this to an .ASHX, and in the ProcessRequest i do:
StreamReader sr = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding);
javaScriptSerializer.Deserialize<MyClass>(message);
and I have no problems.
I don't know how to get the data in PHP, either $_POST and $_REQUEST are empty arrays.
Any help please?
thanks
You've not set a field name for the data being sent. PHP requires data coming in via POST to be in a fieldname=value format. _POST is an array like any other, and every value stored in a PHP array must have a key (which is the form field name).
You can try retrieving the data by reading from php://input:
$post_data = file_get_contents('php://input');
But the simplest solution is to provide a fieldname in your XHR call.
You are setting the Content-Type to application/x-www-form-urlencoded, but you are setting the content to json. I'm guessing this is confusing your PHP webserver. Try setting your POST contents to what you tell the server it will be (application/x-www-form-urlencoded) or read the raw HTTP contents in php://input.
If you set the Content-Type to application/x-www-form-urlencoded then naturally PHP will want to parse the POST body. But as the data does not actually constitute form data, it is discarded as invalid.
You need to set the correct CT application/json (actually doesn't matter) so PHP will leave it alone. Then it becomes available as php://input or $HTTP_RAW_POST_DATA