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?
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.
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.
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]);
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.
I use that to inspect and debug my request payload
$request->getBody()->read($request->getBody()->getSize())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With