Cerbix's avatar

New Laravel HTTP Facades & Verify (guzzle)

So I just updated to the new Laravel 7.2 and I was pretty excited to see the HTTP Facades to reduce/clean up the code required for HTTP requests with Guzzle.

Reducing my code from this:

        $headers = [
            'Accept'        => 'application/json',
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer ' . $apitoken
        ];

        $client = new Client([
            'verify' => false,
            'headers' => $headers
            ]);

        $request = $client->get(url('api/'));
        $response = json_decode($request->getBody()->getContents(), true);
        
        return $response;

To this:

        $response = Http::withHeaders([
            'Accept'        => 'application/json',
            'Content-Type' => 'application/json',

        ])->get(url('api/'));

        $response->throw()->json();
        
        return $response;

But the only thing I can't figure out is how to incorporate the guzzle "Verify" into the new code so that it doesn't throw the SSL Certificate error on my local/build machine.

Kind of annoying always having to go into the Node Modules/Guzzle folder and manually changing it all the time when I build/test things locally.

A second question I would have is how to handle custom Exception Handling. It was much more intuitive to write to custom messages to logs with guzzles "try" "catch" stuff where I could just drop an if statement to send me error messages if a problem arises.

Compared to:

$response->clientError();

How do I customize this similarly?

Thanks in advance.

0 likes
1 reply

Please or to participate in this conversation.