0

I am able to make a post request with guzzle php using the following code

$request = $this->request('POST', $this->url, array('form_params' => $params));

Everything works fine. But when I call

$request->getBody()->getContents();

A string "root" is attached to the beginning of the content returned.

I dont seems to understand why this is happening.

Any assistance will be appreciated.

An example of what I get when I var_dump is this

string(4) "root" 
{"access_token":"kjVbpzmk3VAWTHn3jyeaM1nal1zkFIPZrI8khmKQ",
"token_type":"Bearer",
"expires_in":604800,
"user_id":3,
"user":{
  "id":3,
  "name":"Thomas Paul"
 }
}

Meanwhile in postman I get this

{
 "access_token": "y9Jeovb3EERC4oE13yCS8WfFi3XK1eul4D4luwX3",
 "token_type": "Bearer",
 "expires_in": 604800,
 "user_id": 3,
 "user": {
   "id": 3,
   "name": "Thomas Paul"
 }
}
Algorithm
  • 149
  • 1
  • 8

2 Answers2

0

This is a security measure as there is a known security vulnerability in some browsers.

Also the JSON API specs require this top level element.

Guzzle is a really robust library. Maybe Postman isn't or maybe Postman removes the root element by itself... I don't know this.

Community
  • 1
  • 1
Aerendir
  • 6,152
  • 9
  • 55
  • 108
  • 1
    the root does not appear to be an element. Because if it is it should have : in front of it – Algorithm Dec 22 '15 at 17:32
  • Good observation... Did you tried to get the response without using Guzzle but in pure PHP? What does the API return? – Aerendir Dec 22 '15 at 17:34
  • It appears to be thesame – Algorithm Dec 22 '15 at 17:44
  • Do you mean a bad implementation in the web service or the client – Algorithm Dec 22 '15 at 17:48
  • The webservice... If in pure PHP the response is the same returned by Guzzle, then this is the RAW response returned by the webservice... Try to contact them and ask for clarifications... or search for further info in their documentation... – Aerendir Dec 22 '15 at 17:52
0

I solved the problem using a

substr($request->getBody()->getContents(), 17) 

of the response and was able to remove the unwanted strings and decoded it to json

Algorithm
  • 149
  • 1
  • 8