Currently Available: Need a skilled Software Developer for your next project?
Categories
Laravel

Debugging Laravel HTTP Requests with Dump and DD

When Laravel code calls an external API, Laravel’s HTTP client prepares a request, sends it to the remote server, and receives a response. A request Laravel has prepared but not sent yet is called a pending request. Laravel’s dump() and dd() debugging methods let you inspect the request before it is sent or inspect the response after the server returns it.

dump() displays information and lets execution continue. dd() displays information and stops execution. For a pending request, that distinction also determines whether Laravel sends the call: Http::dump() continues toward the API, while Http::dd() stops before the call completes. These methods help isolate mistakes in request construction and unexpected API responses during development.

Inspect a request before Laravel sends it

Add dump() to the HTTP client chain to inspect the outgoing Laravel request and its request options while allowing the call to continue:

use Illuminate\Support\Facades\Http;

$response = Http::withToken($token)
    ->acceptJson()
    ->dump()
    ->post('https://api.example.test/users', [
        'name' => 'Taylor',
    ]);

The output shows the HTTP method, URL, headers, body or submitted data, and the settings Laravel will use to send the request. Laravel’s HTTP client documentation explains how to dump a request before sending it. The request API also documents methods for checking the request’s method, URL, headers, body, and data.

Use Http::dd() at the same point when you want to inspect the request without sending it:

Http::withToken($token)
    ->acceptJson()
    ->dd()
    ->post('https://api.example.test/users', [
        'name' => 'Taylor',
    ]);

Laravel stops in the pre-send debugging callback, so the API does not receive this call. This is useful when you need to verify a request without triggering an external action. If you need the call to reach the API and want to inspect its result, use Http::dump() instead.

The output shows Laravel’s request and its settings before sending. It does not prove that the remote server received the request or show every change made while the request was sent. If the request looks correct but the server reports something different, check the remote service’s logs or other evidence of what it received.

Inspect the response after the call

Once Laravel receives a response, use methods on the response object to inspect its content or headers:

$response = Http::get('https://api.example.test/users');

$response->dump();        // Shows response content and continues.
$response->dumpHeaders(); // Shows response headers and continues.

Use $response->dd() or $response->ddHeaders() when you want to stop after displaying the response content or headers. These methods inspect a completed call; unlike Http::dd(), they run after Laravel has received a response.

A dump of the response content might not include the status and headers. Dump those fields directly when you need to see them together:

dump(
    $response->status(),
    $response->headers(),
    $response->body(),
);

Laravel also provides methods such as successful(), failed(), clientError(), and serverError() to check the response status. For HTTP error statuses such as 4xx and 5xx, Laravel’s HTTP client returns responses for HTTP error statuses instead of automatically throwing an exception. When an API call appears to fail, check the status as well as the body. The response inspection methods show how to check the response.

Choose the dump that matches the problem

If an API rejects a call, start with Http::dump() to check the method, URL, headers, and submitted data Laravel is preparing to send. If the request looks right, inspect the status, headers, and body in the response to see what the server returned. Use Http::dd() to inspect a request without triggering the remote call. Use response dd() methods to stop execution after Laravel receives a response.

These HTTP-client methods help debug calls Laravel sends to other services. To debug a browser request that comes into Laravel, use a different set of tools. Laravel documents separate debugging tools for HTTP tests.

Treat dump output as sensitive. Request headers and options can include credentials, and response bodies can include private data. Remove debugging calls before deploying code and avoid sharing unredacted output.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *