Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Nicekiwi's avatar

Http::get() returns non-standard response on error

[Laravel 9]

I'm using Illuminate\Support\Facades\Http::get to query an external API, and upon an error it returns a JSON error response.

But when catching exceptions and calling $exception->getMessage() instead of a JSON string, I get a text message describing the error before the JSON.

e.g.

HTTP request returned status code 403: {"error":{"message":"Error message detials","error_code":null}}

I've checked via curl and the API does only return the JSON part. So why is Laravel adding the string HTTP request returned status code 403: and how do I stop it doing so?

0 likes
3 replies
martinbean's avatar
Level 80

@nicekiwi Because that’s the message from the exception. Not the response of the HTTP request.

The getMessage method is for returning a string value describing the exception that was thrown. Which is what you’re getting.

Nicekiwi's avatar

@martinbean Ok, that makes sense. But I understood the default behavior was not to throw an exception? https://laravel.com/docs/9.x/http-client#making-requests "Unlike Guzzle's default behavior, Laravel's HTTP client wrapper does not throw exceptions on client or server errors"

Don't I have to specify $response->throw(); to get an exception? My OP makes it sound like I have, but I haven't. An exception is thrown just from Http::get() ?

martinbean's avatar

@Nicekiwi I don’t know. I’m just going on the information you posted. You didn’t show any code, and said you were catching exceptions and showed calling $exception->getMessage(). Which is a standard method on all exception classes that returns a string description of the exception.

1 like

Please or to participate in this conversation.