Ayyanar G
Ayyanar G

Reputation: 1545

JSON Post data empty - Linux curl Command

I am trying to post json data to php file using linux curl command, (Lamp Server)

$ curl -V -H "Content-Type: application/json" -X POST -d '{"id": "123"}' 
       http://localhost/crm/UpdateUser.php

In UpdateUser.php,

<?php echo var_dump ($_POST);?>

OUTPUT:

[ec2-user@ip-10-35-1-181 ~]$ curl -v -H "Content-Type: application/json" -X POST -d '{"id": "123"}' http://viacrm.odema.net/crm/UpdateUser.php

* Hostname was NOT found in DNS cache
*   Trying 54.217.206.217...   
> POST /crm/UpdateUser.php HTTP/1.1
> User-Agent: curl/7.36.0
> Host: 192.168.1.16
> Accept: */*
> Content-Type: application/json
> Content-Length: 13
>
* upload completely sent off: 13 out of 13 bytes
< HTTP/1.1 200 OK
< Date: Mon, 16 Jun 2014 12:25:00 GMT
* Server Apache/2.2.27 (Amazon) is not blacklisted
< Server: Apache/2.2.27 (Amazon)
< X-Powered-By: PHP/5.3.28
< Content-Length: 13
< Connection: close
< Content-Type: text/html; charset=UTF-8
<
array(0) {
}
* Closing connection 0

Always the Post data shows empty, I even tried to use "ACCEPT: application/json", still same problem. Please can anyone guide this ?

Upvotes: 1

Views: 3074

Answers (2)

mortymacs
mortymacs

Reputation: 3736

Instead of $_POST try it:

<?php
    print($HTTP_RAW_POST_DATA);
?>

Upvotes: 0

ebohlman
ebohlman

Reputation: 15003

$_POST only contains the results of decoding an application/x-www-form-urlencoded request. You need to read the raw request body. If you have the always_populate_raw_post_data configuration directive turned on, then the raw body will be in $HTTP_RAW_POST_DATA; otherwise you can obtain it by reading from the php://input stream.

Upvotes: 3

Related Questions