diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index 91ce36673a1..ac3ec779106 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -6,6 +6,7 @@ use GuzzleHttp\Cookie\CookieJar; use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\HandlerStack; +use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; class PendingRequest @@ -466,8 +467,22 @@ public function send(string $method, string $url, array $options = []) return retry($this->tries ?? 1, function () use ($method, $url, $options) { try { + $laravelData = $options[$this->bodyFormat] ?? $options['query'] ?? []; + + // Attempt to parse query string on url if no request data found + $urlStr = Str::of($url); + if (! $laravelData && $method === 'GET' && $urlStr->contains('?')) { + $laravelData = (string) $urlStr->after('?'); + } + + // If found data is a string then treat it as query parameters and parse them to an array + if (is_string($laravelData)) { + parse_str($laravelData, $parsedData); + $laravelData = is_array($parsedData) ? $parsedData : []; + } + return tap(new Response($this->buildClient()->request($method, $url, $this->mergeOptions([ - 'laravel_data' => $options[$this->bodyFormat] ?? [], + 'laravel_data' => $laravelData, 'on_stats' => function ($transferStats) { $this->transferStats = $transferStats; }, diff --git a/src/Illuminate/Http/Client/Request.php b/src/Illuminate/Http/Client/Request.php index 5cc7ae588dc..812fc4568e7 100644 --- a/src/Illuminate/Http/Client/Request.php +++ b/src/Illuminate/Http/Client/Request.php @@ -3,6 +3,7 @@ namespace Illuminate\Http\Client; use ArrayAccess; +use Illuminate\Support\Arr; use Illuminate\Support\Str; use LogicException; @@ -66,9 +67,15 @@ public function hasHeader($key, $value = null) return ! empty($this->request->getHeaders()[$key]); } + $headers = $this->headers(); + + if (! Arr::has($headers, $key)) { + return false; + } + $value = is_array($value) ? $value : [$value]; - return empty(array_diff($value, $this->headers()[$key])); + return empty(array_diff($value, $headers[$key])); } /** @@ -79,7 +86,7 @@ public function hasHeader($key, $value = null) */ public function header($key) { - return $this->headers()[$key]; + return Arr::get($this->headers(), $key, [null]); } /** diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 9df1b53676a..4648ea2a2a6 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -269,7 +269,8 @@ public function testGetWithArrayQueryParam() $this->factory->get('http://foo.com/get', ['foo' => 'bar']); $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/get?foo=bar'; + return $request->url() === 'http://foo.com/get?foo=bar' + && $request['foo'] === 'bar'; }); } @@ -280,7 +281,8 @@ public function testGetWithStringQueryParam() $this->factory->get('http://foo.com/get', 'foo=bar'); $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/get?foo=bar'; + return $request->url() === 'http://foo.com/get?foo=bar' + && $request['foo'] === 'bar'; }); } @@ -291,7 +293,9 @@ public function testGetWithQuery() $this->factory->get('http://foo.com/get?foo=bar&page=1'); $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/get?foo=bar&page=1'; + return $request->url() === 'http://foo.com/get?foo=bar&page=1' + && $request['foo'] === 'bar' + && $request['page'] === '1'; }); } @@ -302,7 +306,10 @@ public function testGetWithQueryWontEncode() $this->factory->get('http://foo.com/get?foo;bar;1;5;10&page=1'); $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/get?foo;bar;1;5;10&page=1'; + return $request->url() === 'http://foo.com/get?foo;bar;1;5;10&page=1' + && ! isset($request['foo']) + && ! isset($request['bar']) + && $request['page'] === '1'; }); } @@ -313,7 +320,8 @@ public function testGetWithArrayQueryParamOverwrites() $this->factory->get('http://foo.com/get?foo=bar&page=1', ['hello' => 'world']); $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/get?hello=world'; + return $request->url() === 'http://foo.com/get?hello=world' + && $request['hello'] === 'world'; }); } @@ -324,7 +332,8 @@ public function testGetWithArrayQueryParamEncodes() $this->factory->get('http://foo.com/get', ['foo;bar; space test' => 'laravel']); $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/get?foo%3Bbar%3B%20space%20test=laravel'; + return $request->url() === 'http://foo.com/get?foo%3Bbar%3B%20space%20test=laravel' + && $request['foo;bar; space test'] === 'laravel'; }); } }