5

I'm just trying to send a POST request with JS to server. But server has empty $_POST array. I could use HTTP_RAW_POST_DATA, but it'll be deprecated in PHP 5.6. Could I have posted data in my $_POST array?

Environment: Chrome, apache2, PHP, AngularJS (I'm using $http.post function).

Debug image (sorry for not attaching image directly - I have no 10 reputation)

2
  • 1
    Try adding headers: {'Content-Type': 'application/x-www-form-urlencoded'}, to your $http.post? Commented Nov 14, 2014 at 11:00
  • See my Debug image for code. I tried {'Content-Type': 'application/x-www-form-urlencoded'} but in this case I have no neither $_POST nor HTTP_RAW_POST_DATA on server Commented Nov 14, 2014 at 11:21

4 Answers 4

11

The POST data must be in query string or multipart/form-data format to get decoded properly. Your data seems to be JSON, so you have to decode it by yourself:

$_POST = json_decode(file_get_contents('php://input'), true);
Sign up to request clarification or add additional context in comments.

2 Comments

Although, to exactly use it like $_POST we should add true as second parameter to json_decode so that it returns an array. $_POST = json_decode(file_get_contents('php://input'), true);
You're right. Thank you for your comment. Answer edited.
4

$_POST is populated by a request that is of type form-urlencoded or multipart/form-data. Typically it looks like:

foo=bar&ipsum=lorm

So kinda like a GET request.

Since you're posting JSON directly (which is awesome!) you can use:

$request_payload = file_get_contents("php://input");

See the docs for more info.

3 Comments

Why is posting JSON directly awesome? (curiosity, no offense)
Because using form encoding on JSON data is problematic. Specifically empty values do not get transmitted. {"foo":[],"bar:"baz"} will result in just bar=baz. Form encoding has no way of specifying an empty array.
A missing empty array may not seem like a problem because you can add it as a default value. But, if you have: {foo:[[],[]]} form encoding gives an empty result. I think that's a problem.
3

See by default, jQuery transmits data using Content-Type: x-www-form-urlencoded and the familiar foo=bar&baz=moe serialization. AngularJS, however, transmits data using Content-Type: application/json and { "foo": "bar", "baz": "moe" } JSON serialization, which unfortunately some Web server languages—notably PHP—do not unserialize natively.

so you can do this when you define your angular module:

angular.module('MyModule', [], function($httpProvider) {
    // Use x-www-form-urlencoded Content-Type
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
});

answer taken from this post by Felipe Miosso.

3 Comments

Yeah, it's working. But I don't know, why direct setting 'Content-Type' in $http.post(filtersUrl, data, {headers: {'Content-Type': 'application/json; charset=utf-8'}) is not working... I've did $http.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8' and it's also working
$http.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8' also does the same thing, but providing as suggested is available in the whole application i think.
And yes, in this case I was needed to do json_decode(file_get_contents('php://input')); on my server...
0

looks like json data posting directly without any variable try with

$request = file_get_contents('php://input');
print_r($request);

or use a variable on posting data like

data{'myvar': data}

and will get on POST data like

print_r($_POST['myvar']);

1 Comment

Both are not working: 1) file_get_contents("php://input") gives empty string; 2) {'myvar': data} has the same result as original post data ($_POST is empty)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.