GuzzleHttp\Client ignores base path in base_url

Benjamin Nolan picture Benjamin Nolan · Aug 12, 2014 · Viewed 7.1k times · Source

I'm using Guzzle in a set of PHPUnit-powered tests for a REST API.

I create my client as follows:

use GuzzleHttp\Client;

$client = new Client(['base_url' => ['http://api.localhost/api/{version}', ['version' => '1.0']]]);

This works fine, and I can make requests using the following code:

$request = $client->createRequest('GET', '/auth');
$request->setBody(Stream::factory(json_encode(['test'=>'data'])));
$response = $client->send($request);
$decodedResponse = $response->json();

However, Guzzle is ignoring the /api/{version} part of the base URL and makes the request to here:

http://api.localhost/auth

However, I would have expected it to make the request here:

http://api.localhost/api/1.0/auth

Have I mis-read the documentation and my expected behaviour is thus wrong, or is there some other option I need to enable to get it to append the /auth url to the /api/1.0 base path when making the request?

Answer

Michael Dowling picture Michael Dowling · Aug 13, 2014

You're using an absolute path in your requests, so it's overriding the path set in the base URL. Guzzle follows RFC 3986 when combining URLs: http://tools.ietf.org/html/rfc3986#section-5.2