Guzzle 6: No More JSON() Method for Responses

Guzzle 6: no more json() method for responses

I use json_decode($response->getBody()) now instead of $response->json().

I suspect this might be a casualty of PSR-7 compliance.

How to return Guzzle JSON response

After some more research on SO I tumbled head first into this post

Guzzle 6: no more json() method for responses

Essentially doing the following will return the raw output.

return $response->getBody()->getContents();

Huge headache gone. Hope this helps someone

Guzzle returns stream empty body instead of json body

getBody() returns a stream. If you want to get all the content at once you can use getContents() method and decode json while at it (if you need to)

$payload = json_decode($response->getBody()->getContents());

Further reading - Guzzle Responses

Guzzle 6 Post upgrade

You need this:

$response = $this->guzzle6->post(
'/login?token='.$this->container->getParameter("token"),
[
'json' => $data
]
);

return json_decode($response->getBody()->getContents());

Guzzle 6 doesn't have ->json() for responses, so you have to decode it by yourself.

Guzzlehttp - How get the body of a response from Guzzle 6?

Guzzle implements PSR-7. That means that it will by default store the body of a message in a Stream that uses PHP temp streams. To retrieve all the data, you can use casting operator:

$contents = (string) $response->getBody();

You can also do it with

$contents = $response->getBody()->getContents();

The difference between the two approaches is that getContents returns the remaining contents, so that a second call returns nothing unless you seek the position of the stream with rewind or seek .

$stream = $response->getBody();
$contents = $stream->getContents(); // returns all the contents
$contents = $stream->getContents(); // empty string
$stream->rewind(); // Seek to the beginning
$contents = $stream->getContents(); // returns all the contents

Instead, usings PHP's string casting operations, it will reads all the data from the stream from the beginning until the end is reached.

$contents = (string) $response->getBody(); // returns all the contents
$contents = (string) $response->getBody(); // returns all the contents

Documentation: http://docs.guzzlephp.org/en/latest/psr7.html#responses

guzzle 6 post method is not working

this is my mistake i am passing wrong date for-mate

"posting_date" => "31\/07\/2014",
"expiry_date" => "31\/08\/2014",

instead of this i have pass

"posting_date" => "31/07/2014",
"expiry_date" => "31/08/2014",

then is working



Related Topics



Leave a reply



Submit