Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inspect the payload of a guzzle request

Guzzle includes a simple NodeJS server for testing, and I am using that to mock my backend. The server contains a getReceivedRequests method which returns a list of all the requests that it had received, and I am trying to inspect the requests to make sure my program is sending them correctly.

For GET requests it's easy enough. Looking at the Request class, I can get the query parameters and assert on them.

The question comes when I am trying to inspect a PUT request. Looking at the Request class there seems to be no way for me to get its payload. In fact I don't even know where the payload is stored in the object, as there is no private fields indicated as such.

Is there anyway I can assert on the payload of a guzzle request?

like image 610
Xavier_Ex Avatar asked Mar 18 '15 22:03

Xavier_Ex


People also ask

How do I get data from guzzle response?

As described earlier, you can get the body of a response using the getBody() method. Guzzle uses the json_decode() method of PHP and uses arrays rather than stdClass objects for objects. You can use the xml() method when working with XML data.

How do I debug guzzle request?

Debugging when using Guzzle, is quiet easy by providing the debug key in the payload: $client->request('GET', '/url, ['debug' => true]); This is quiet easy and not an issue if your are not passing any body content, using only query string to dump what's been request.

How do you send a POST request on GuzzleHttp?

Sending Requests You can create a request and then send the request with the client when you're ready: use GuzzleHttp\Psr7\Request; $request = new Request('PUT', 'http://httpbin.org/put'); $response = $client->send($request, ['timeout' => 2]);

What is the difference between cURL and guzzle?

cURL can be classified as a tool in the "File Transfer" category, while Guzzle is grouped under "Microframeworks (Backend)". cURL and Guzzle are both open source tools. Guzzle with 17.1K GitHub stars and 1.95K forks on GitHub appears to be more popular than cURL with 14K GitHub stars and 3.09K GitHub forks.


1 Answers

I use that to inspect and debug my request payload

$request->getBody()->read($request->getBody()->getSize())

like image 107
guillaumepotier Avatar answered Sep 20 '22 19:09

guillaumepotier