Skip to content

Commit 2c8182c

Browse files
authored
[7.x] Http client query string support (#31996)
* Add support for string and array both type for query param. This gives more control over the URL query params just the way Guzzle is designed and supports. * Remove unwanted method. No longer needed. * Add tests
1 parent a1a1dbf commit 2c8182c

File tree

2 files changed

+68
-16
lines changed

2 files changed

+68
-16
lines changed

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,10 @@ public function beforeSending($callback)
376376
* Issue a GET request to the given URL.
377377
*
378378
* @param string $url
379-
* @param array $query
379+
* @param array|string|null $query
380380
* @return \Illuminate\Http\Client\Response
381381
*/
382-
public function get(string $url, array $query = [])
382+
public function get(string $url, $query = null)
383383
{
384384
return $this->send('GET', $url, [
385385
'query' => $query,
@@ -468,7 +468,6 @@ public function send(string $method, string $url, array $options = [])
468468
try {
469469
return tap(new Response($this->buildClient()->request($method, $url, $this->mergeOptions([
470470
'laravel_data' => $options[$this->bodyFormat] ?? [],
471-
'query' => $this->parseQueryParams($url),
472471
'on_stats' => function ($transferStats) {
473472
$this->transferStats = $transferStats;
474473
},
@@ -604,19 +603,6 @@ public function mergeOptions(...$options)
604603
return array_merge_recursive($this->options, ...$options);
605604
}
606605

607-
/**
608-
* Parse the query parameters in the given URL.
609-
*
610-
* @param string $url
611-
* @return array
612-
*/
613-
public function parseQueryParams(string $url)
614-
{
615-
return tap([], function (&$query) use ($url) {
616-
parse_str(parse_url($url, PHP_URL_QUERY), $query);
617-
});
618-
}
619-
620606
/**
621607
* Register a stub callable that will intercept requests and be able to return stub responses.
622608
*

tests/Http/HttpClientTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,70 @@ public function testWithCookies()
223223
$this->assertSame('bar', $responseCookie['Value']);
224224
$this->assertSame('https://laravel.com', $responseCookie['Domain']);
225225
}
226+
227+
public function testGetWithArrayQueryParam()
228+
{
229+
$this->factory->fake();
230+
231+
$this->factory->get('http://foo.com/get', ['foo' => 'bar']);
232+
233+
$this->factory->assertSent(function (Request $request) {
234+
return $request->url() === 'http://foo.com/get?foo=bar';
235+
});
236+
}
237+
238+
public function testGetWithStringQueryParam()
239+
{
240+
$this->factory->fake();
241+
242+
$this->factory->get('http://foo.com/get', 'foo=bar');
243+
244+
$this->factory->assertSent(function (Request $request) {
245+
return $request->url() === 'http://foo.com/get?foo=bar';
246+
});
247+
}
248+
249+
public function testGetWithQuery()
250+
{
251+
$this->factory->fake();
252+
253+
$this->factory->get('http://foo.com/get?foo=bar&page=1');
254+
255+
$this->factory->assertSent(function (Request $request) {
256+
return $request->url() === 'http://foo.com/get?foo=bar&page=1';
257+
});
258+
}
259+
260+
public function testGetWithQueryWontEncode()
261+
{
262+
$this->factory->fake();
263+
264+
$this->factory->get('http://foo.com/get?foo;bar;1;5;10&page=1');
265+
266+
$this->factory->assertSent(function (Request $request) {
267+
return $request->url() === 'http://foo.com/get?foo;bar;1;5;10&page=1';
268+
});
269+
}
270+
271+
public function testGetWithArrayQueryParamOverwrites()
272+
{
273+
$this->factory->fake();
274+
275+
$this->factory->get('http://foo.com/get?foo=bar&page=1', ['hello' => 'world']);
276+
277+
$this->factory->assertSent(function (Request $request) {
278+
return $request->url() === 'http://foo.com/get?hello=world';
279+
});
280+
}
281+
282+
public function testGetWithArrayQueryParamEncodes()
283+
{
284+
$this->factory->fake();
285+
286+
$this->factory->get('http://foo.com/get', ['foo;bar; space test' => 'laravel']);
287+
288+
$this->factory->assertSent(function (Request $request) {
289+
return $request->url() === 'http://foo.com/get?foo%3Bbar%3B%20space%20test=laravel';
290+
});
291+
}
226292
}

0 commit comments

Comments
 (0)