Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

base_uri not being based from guzzle client instantiation

Tags:

guzzle

lumen

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

like image 456
drrobotnik Avatar asked Jun 14 '15 20:06

drrobotnik


People also ask

How do you set headers in guzzle?

Each key is the name of a header, and each value is a string or array of strings representing the header field values. // Set various headers on a request $client->request('GET', '/get', [ 'headers' => [ 'User-Agent' => 'testing/1.0', 'Accept' => 'application/json', 'X-Foo' => ['Bar', 'Baz'] ] ]);

How do I send HTTP request using guzzle?

Sending Requests You can create a request and then send the request with the client when you're ready: use GuzzleHttp\Psr7\Request; $request = new Request('PUT', 'http://httpbin.org/put'); $response = $client->send($request, ['timeout' => 2]);

How do I debug guzzle request?

Debugging when using Guzzle, is quiet easy by providing the debug key in the payload: $client->request('GET', '/url, ['debug' => true]); This is quiet easy and not an issue if your are not passing any body content, using only query string to dump what's been request.


1 Answers

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.

like image 82
Avindra Goolcharan Avatar answered Sep 28 '22 04:09

Avindra Goolcharan