Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GuzzleHttp\Client ignores base path in base_url

Tags:

php

guzzle

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?

like image 999
Benjamin Nolan Avatar asked Aug 12 '14 15:08

Benjamin Nolan


Video Answer


1 Answers

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: https://www.rfc-editor.org/rfc/rfc3986#section-5.2

like image 63
Michael Dowling Avatar answered Oct 07 '22 01:10

Michael Dowling