0

I'm using curl to post json data to a rest endpoint which is in php. This is being done in an integration test. Surprisingly, entire json object is available to me in $_POST on the server. How is this possible ? Here's my php code which posts the json :

$data = array(
            'username' => 'blahbah',
            'password' => 'blahblah',
            'grant_type' => 'client_credentials',
            'client_id' => 'blah',
            'client_secret' => 'blah'
        );
        $data_string = json_encode($data);

        $this->curl = curl_init($this->tokenApi);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($this->curl, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string));
        $response = curl_exec($this->curl);

On the api side , if I do a print_r($_POST), I get:

Array
(
    [username] => blahbah
    [password] => blahblah
    [grant_type] => client_credentials
    [client_id] => blah
    [client_secret] => blah
)

How is this possible ? From what I have read, the json string should only be available as "php//input". I'm a bit confused about what's the correct way to access the json. I'm using PHP 5.4.

Thanks.

lazy python
  • 179
  • 1
  • 10
  • Why should that json string _not_ be available in the `$_POST` superglobal? You send it as a post value. – arkascha Jul 05 '15 at 07:00
  • Even when Content-Type is set as application/json ? – lazy python Jul 05 '15 at 07:01
  • Why should that make a difference? Note that you are talking about a _http request_ here, not an http response! Your http server will hand the request payload over to a php script if such is called. Except if you have somehow configured yout http server not to do so. PHP does not see a difference here anyway. – arkascha Jul 05 '15 at 07:03
  • Maybe you are comparing this to the behavior you can see in for example jqueries ajax convenience functions, which hand out decoded json if the type is specified as such for the request. But that is simply a matter of the implementation of those convenience functions. You can do the same in php if you like. Or you just take the json string from the post values and decode it. I fail to see an issue here... – arkascha Jul 05 '15 at 07:04
  • Thanks @arkascha. I read somewhere on stackoverflow itself that $_POST is populated only when content-type is 'application/x-www-form-urlencoded'. Hence my confusion. – lazy python Jul 05 '15 at 07:20
  • Just found the link : http://stackoverflow.com/questions/19004783/reading-json-post-using-php – lazy python Jul 05 '15 at 07:20
  • Can't say much more... Apparently you _do_ have the payload in your `$_POST` superglobal. – arkascha Jul 05 '15 at 07:21
  • 1
    Actually the answer to your question is written in that older question you cite... "CURLOPT_POSTFIELDS tells curl to encode your parameters as application/x-www-form-urlencoded" – arkascha Jul 05 '15 at 07:23
  • Cool .. Thanks a lot @arkascha. – lazy python Jul 05 '15 at 07:25
  • Welcome! Have fun today! – arkascha Jul 05 '15 at 07:25
  • One of those days !! ( where you feel like such an idiot!! ) :) – lazy python Jul 05 '15 at 07:28

0 Answers0