Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1cbefd7

Browse filesBrowse files
sleepyboynicolas-grekas
authored andcommitted
[HttpClient] Allow arrays as query parameters
1 parent aedd52e commit 1cbefd7
Copy full SHA for 1cbefd7

File tree

2 files changed

+16
-9
lines changed
Filter options

2 files changed

+16
-9
lines changed

‎src/Symfony/Component/HttpClient/HttpClientTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/HttpClientTrait.php
+12-9Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -481,17 +481,20 @@ private static function mergeQueryString(?string $queryString, array $queryArray
481481
}
482482
}
483483

484-
foreach ($queryArray as $k => $v) {
485-
if (is_scalar($v)) {
486-
$queryArray[$k] = rawurlencode($k).'='.rawurlencode($v);
487-
} elseif (null === $v) {
488-
unset($queryArray[$k]);
489-
490-
if ($replace) {
484+
if ($replace) {
485+
foreach ($queryArray as $k => $v) {
486+
if (null === $v) {
491487
unset($query[$k]);
492488
}
493-
} else {
494-
throw new InvalidArgumentException(sprintf('Unsupported value for query parameter "%s": scalar or null expected, %s given.', $k, \gettype($v)));
489+
}
490+
}
491+
492+
$queryString = http_build_query($queryArray, '', '&', PHP_QUERY_RFC3986);
493+
$queryArray = [];
494+
495+
if ($queryString) {
496+
foreach (explode('&', $queryString) as $v) {
497+
$queryArray[rawurldecode(explode('=', $v, 2)[0])] = $v;
495498
}
496499
}
497500

‎src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ public function provideParseUrl()
141141
yield [[null, null, 'bar', '?a=1&c=c', null], 'bar?a=a&b=b', ['b' => null, 'c' => 'c', 'a' => 1]];
142142
yield [[null, null, 'bar', '?a=b+c&b=b', null], 'bar?a=b+c', ['b' => 'b']];
143143
yield [[null, null, 'bar', '?a=b%2B%20c', null], 'bar?a=b+c', ['a' => 'b+ c']];
144+
yield [[null, null, 'bar', '?a%5Bb%5D=c', null], 'bar', ['a' => ['b' => 'c']]];
145+
yield [[null, null, 'bar', '?a%5Bb%5Bc%5D=d', null], 'bar?a[b[c]=d', []];
146+
yield [[null, null, 'bar', '?a%5Bb%5D%5Bc%5D=dd', null], 'bar?a[b][c]=d&e[f]=g', ['a' => ['b' => ['c' => 'dd']], 'e[f]' => null]];
147+
yield [[null, null, 'bar', '?a=b&a%5Bb%20c%5D=d&e%3Df=%E2%9C%93', null], 'bar?a=b', ['a' => ['b c' => 'd'], 'e=f' => '']];
144148
}
145149

146150
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.