Junior Frogie
Junior Frogie

Reputation: 313

Laravel Guzzle HTTP keep returning 404

I'm using openlibrary's API to fetch book data. I have problem with some API (not all) which keep return 404, but when i'm using postman to test, it's working fine.

$response = Http::get('http://openlibrary.org/search.json', [
'q' => 'Johngreen'
]);
dd($response->body());

Upvotes: 0

Views: 2009

Answers (4)

bhucho
bhucho

Reputation: 3420

openlibrary is not accepting guzzlehttp user agent, try using only curl or a browser user agent.
Both of these works

using curl user agent


try{
     $response = Http::withHeaders([  
         'User-Agent' => 'curl/7.65.3'
     ])->get('https://openlibrary.org/search.json?q=Johngreen');
     dd($response->body());
} catch(\Illuminate\Http\Client\RequestException $e){
     // Log your errors
}

using browser user agent

try{
      $response = Http::withHeaders([
          'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'   
      ])->get('https://openlibrary.org/search.json?q=Johngreen');
      dd($response->body());
} catch(\Illuminate\Http\Client\RequestException $e){
      // Log your errors
}

For more info on default user agent used by guzzle, see docs

Upvotes: 1

Jaswinder Singh
Jaswinder Singh

Reputation: 731

You are using Guzzle. Try using the below code for consuming API.

$client = new \GuzzleHttp\Client();
$response = $client->get('http://openlibrary.org/search.json', [
    'query' => ['q' => 'Johngreen']
]);
dd(json_decode($response->getBody()));

Upvotes: 0

Stefano
Stefano

Reputation: 178

I tested this endpoint with Laravel and I had the same problem but it works on Postman and browser.

The problem is related to the User-Agent of the client. If you don't set it, Openlibrary cannot check the source of the API call (browsers and Postman send the own user agent).

I solved with this code:

$guzzle_client = new \GuzzleHttp\Client();

$response = $this->guzzleClient->get("http://openlibrary.org/search.json?q=Johngreen", [
    'headers' => ['User-Agent' => 'PUT AN USER AGENT HERE']
]);
        
$response_body = json_decode($response->getBody()->getContents(), true);
dd($response_body);

Try to use this User Agent:

Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36

or use another one

Upvotes: 2

hery
hery

Reputation: 32

Try code below first to view response all content.

Http::dd()->get('http://openlibrary.org/search.json', [
  'q' => 'Johngreen'
]);

Upvotes: 1

Related Questions