Guzzle 6, get request string

Guzzle 6, get request string

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Inspecting Guzzle 6 Request Strings for Debugging 403 Errors

When a POST request to an endpoint returns a 403 Forbidden, the immediate suspicion often falls on authentication or headers. However, if you are confident your credentials are correct—especially since the identical request succeeds in Postman—the issue usually lies in how the HTTP client serializes the body or constructs the URL. In Guzzle 6, the default behavior is to encode query parameters and form data based on specific conventions that might differ from manual tooling.

To inspect the exact string being sent before it hits the wire, you need to intercept the request object within the transfer process. The most direct approach in Guzzle 6 is to use a middleware or an event listener attached to your client instance. This allows you to capture the RequestInterface object right before it is executed.

You can attach a middleware that logs the URI and body content. Here is how you might configure a custom middleware to print the request details:

use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;

$client = new \GuzzleHttp\Client();

$requestLogger = Middleware::http('request', function ($event) {
    /** @var RequestInterface $request */
    $request = $event['request'];
    
    // Log the full request string including headers and body
    echo $request->getUri()->__toString() . PHP_EOL;
    echo 'Method: ' . $request->getMethod() . PHP_EOL;
    echo 'Headers:' . PHP_EOL;
    foreach ($request->getHeaders() as $name => $values) {
        echo "  {$name}: " . implode(', ', $values) . PHP_EOL;
    }
    echo 'Body: ' . $request->getBody() . PHP_EOL;
    
    return $event['promise'];
});

$client->getConfig('http_errors') = false; // Optional: prevent automatic throwing of exceptions to control flow manually

$res = $client->withMiddleware($requestLogger)
    ->request('POST', 'https://endpoint.nz/test', [
        'form_params' => ['param1' => 1, 'param2' => 2, 'param3' => 3]
    ]);

By wrapping the client with this middleware, you bypass the need to parse the response immediately. This gives you visibility into exactly what Guzzle constructs: whether it is sending application/x-www-form-urlencoded or expecting a raw string, and how it handles special characters in your parameters.

A common pitfall when comparing Postman to code is the content type header. If you send form parameters using 'form_params', Guzzle automatically sets Content-Type: application/x-www-form-urlencoded. However, some endpoints expect JSON (application/json) or a specific charset. Postman often defaults to multipart/form-data for file uploads or complex structures, but simple key-value pairs usually result in URL-encoded strings. If the endpoint expects JSON but receives form data, a 403 is a likely outcome. You can override this by explicitly setting the headers in your request options:

'request' => [
    'headers' => ['Content-Type' => 'application/json'],
    'json' => ['param1' => 1, 'param2' => 2] // Note: using 'json' sends JSON automatically
],

Using the 'json' option is often safer than 'form_params' when the API expects a JSON payload. Guzzle will serialize the array into a valid JSON string and set the correct headers for you, eliminating ambiguity about content type or character encoding.

Another potential issue is query string parameterization. If your URL contains parameters like ?id=123, ensure they are not being double-encoded or appended incorrectly when combined with POST data. The URI object in Guzzle handles this robustly, but inspecting the $request->getUri()->__toString() output confirms that the final URL matches your expectation.

Finally, consider checking for default middleware like GuzzleHttp\Middleware::history if you are using a larger application stack. Sometimes, plugins or legacy code inject headers like Referer or User-Agent that trigger security filters on the server side. Logging the full header collection is essential to rule out these invisible triggers before assuming your payload is malformed.

Our network