GuzzleHttp \ Client ignores the base path in base_url

I use Guzzle in the PHPUnit test suite for the 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 ignores the /api/{version} base URL and makes the request here:

 http://api.localhost/auth 

However, I would expect it to make a request here:

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

Have I really read the documentation wrong, and I suppose this is a wrong behavior, or is there some other parameter, do I need to enable it to add /auth url to it to the base path /api/1.0 when executing the request?

+7
php guzzle
source share
1 answer

Your requests use an absolute path, so it overrides the path given in the base URL. Guzzle follows RFC 3986 when combining URLs: http://tools.ietf.org/html/rfc3986#section-5.2

+9
source

All Articles