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 7f43878

Browse filesBrowse files
bug #31219 [HttpClient] Allow arrays as query parameters (sleepyboy)
This PR was submitted for the master branch but it was merged into the 4.3 branch instead (closes #31219). Discussion ---------- [HttpClient] Allow arrays as query parameters | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | N/A This PR allows passing arrays as query parameters. For instance, if I pass $options['query']['category'] = ['news', 'sport', 'culture'] to my GET request, I expect this to be transformed to URL?category[]=news&category[]=sport&category[]=culture. I added two tests to cover this functionality. I also fixed one of the tests where "+" wasn't encoded as %20 in the final URL. Commits ------- 1cbefd7 [HttpClient] Allow arrays as query parameters
2 parents aedd52e + 1cbefd7 commit 7f43878
Copy full SHA for 7f43878

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.