17

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 noticeable 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

Avindra Goolcharan
  • 4,032
  • 3
  • 41
  • 39
drrobotnik
  • 739
  • 1
  • 11
  • 25
  • 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 Jun 17 '15 at 10:47
  • 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 Jun 17 '15 at 12:51
  • @PaulZepernick Did the fix I suggested work for you? – Avindra Goolcharan Jun 17 '15 at 14:53
  • @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 http://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 Jun 17 '15 at 17:04

1 Answers1

46

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.

Avindra Goolcharan
  • 4,032
  • 3
  • 41
  • 39
  • 6
    Your last comment is what saved me. Confirmed that using `base_url` with a forward slash ending the url works with Guzzle 5 – Dylan Pierce Jun 26 '15 at 15:35
  • This doesn't work for me. I'm using Guzzle 6.3.3. My client is created as follows (sensitive data obfuscated) `$client = new GuzzleHttp\Client([ 'base_uri' => 'https://****.corehr.com/ws/****/corehr/' ]);` Then I create a request as follows $request = GuzzleHttp\Psr7\Request( 'GET', '/v1/filter/active/' ); Then when I make a call like `$client->send($request)` the endpoint called is `https://****.corehr.com/v1/filter/active` instead of `https://****.corehr.com/ws/****/corehr/v1/filter/active`. – giuliot Nov 15 '18 at 12:54
  • 6
    Sorry, disregard my last comment. The problem is that the relative url must not start with `/`. So `$request = GuzzleHttp\Psr7\Request( 'GET', 'v1/filter/active/' );` works as expected. – giuliot Nov 15 '18 at 13:58