16
votes

I'm using lumen trying to set up simple api requests via guzzle.

The problem is the base_uri parameter doesn't appear to be passed correctly on the initial new Client().

Simplified example:

use GuzzleHttp\Client;

$client = new Client([
    'base_uri' => 'https://siteurl.com/api/v2'
]);

Then calling the api via get

$res = $client->get('orders', [
    'query' => [
        'status' => 'completed'
    ]
]);

does not work. I've been careful not to use absolute urls like /orders. If I bypass base_uri entirely and just add it on the get method $client->get('https://siteurl.com/api/v2/orders'), it works.

I'm using: "laravel/lumen-framework": "5.0.*", "guzzlehttp/guzzle": "^6.0"

*Follow-up:

I added the debug flag so I could compare the headers, and the noticable difference is in the get request line.

Absolute url in the get method (bypassing base_uri):

GET /api/v2/orders?status=completed HTTP/1.1

Using base_uri (version is being stripped):

GET /api/orders?status=completed HTTP/1.1

1
Did you happen to figure this out? I am having the exact same issue, and I do have my base_uri terminated with a / as suggested in the answer.Paul Zepernick
Disregard my comment. I was looking at the wrong documentation for my guzzle version. The newest version uses base_uri and I am back on a previous version that used base_url instead.Paul Zepernick
@PaulZepernick Did the fix I suggested work for you?Avindra Goolcharan
@AvindraGoolcharan I needed to use base_url instead of base_uri. I was looking at the latest doc which says to use base_uri, but I am using version 5.3 so I needed to look at this doc docs.guzzlephp.org/en/5.3 which says to use base_url. The key was changed from base_url -> base_uri in the new version.Paul Zepernick

1 Answers

41
votes

You need to terminate your base_uri with a forward slash /

E.g.,

use GuzzleHttp\Client;

$client = new Client([
    'base_uri' => 'https://siteurl.com/api/v2/'
]);

Edit: Note that base_uri is for Guzzle 6+, whereas previous versions used base_url.