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

ritey's avatar

Form Request, 422 Error and Guzzle

Has anyone worked with Form Requests and a Guzzle client request? I'm trying to access the errors collection being returned by Laravel via the wantsJson() JsonResponse($errors,422) response and I'm seeing the 422 and Unprocessable Entity string but I can't see a way of accessing the errors array.

The documentation from Guzzle suggests a GuzzleHttp\Exception\ClientException will be thrown for the 400 errors but there is no mention of a getBody() on the getResponse() method.

I've found lots of jquery/ajax examples of catching the errors but not via a curl method.

Thanks

0 likes
2 replies
ritey's avatar
ritey
OP
Best Answer
Level 2

Discovered the answer: json_decode($response->getBody()->getContents()); when I was using $response->json(); on a successful response.

1 like
oes's avatar

I had the same problem today but will post the answer. @rity was on the right track but he does say about it only being for the success response. You have to catch errors with the try{}catch() Exception like below.

This is for Gulp version 6.

    private function example($params)
    {
        try {
            $request = $this->client->post('endpoint', $params);
            $this->response = json_decode($request->getBody()->getContents());
            return true;
        } catch (\GuzzleHttp\Exception\RequestException $e) {
            $this->errors = json_decode($e->getResponse()->getBody()->getContents());
        }

        return false;
    }

Please or to participate in this conversation.