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

smokie's avatar

Why does Guzzle return the http status text?

I'm making a gateway with Laravel. It is distributed to other services with the requests coming on the gateway. I am facing a problem here. HTTP status text is also coming and I want to remove it.

HTTP/1.0 422 Unprocessable Content Cache-Control: no-cache, private Content-Type: application/json Date: Mon, 29 Aug 2022 21:01:00 GMT

{"message":"The name field is required. (and 4 more errors)","errors":{"name":["The name field is
required."],"password":["The password field is required."],"email":["The email field is required."],"domain":["The
domain field is required."],"type":["The type field is required."]}}

My code

 private function setRequest($method, $requestUrl, $formParams = [], $headers = []) : mixed
    {
        $client = new Client([
            'verify' => false,
            'base_uri' => $this->serviceUrl
        ]);

        if (isset($this->secret)) {
            $headers['Authorization'] = $this->secretKey;
        }

        if(empty($headers) || !isset($headers['X-Requested-With'])) {
            $headers['X-Requested-With'] = 'XMLHttpRequest';
        }

        try {
            $response = $client->request($method, $requestUrl,
                [
                    'form_params' => $formParams,
                    'headers' => $headers
                ]
            );
        } catch (\GuzzleHttp\Exception\ClientException $exception) {
            return response()->json(json_decode($exception->getResponse()->getBody()->getContents()))->setStatusCode($exception->getResponse()->getStatusCode());
        } catch (\GuzzleHttp\Exception\ServerException $exception) {
            return $this->error(['server error'], $exception->getResponse()->getStatusCode());
        }


        $this->response = $response;

        return $this;
    }
0 likes
0 replies

Please or to participate in this conversation.