Skip to content

Commit 77a9bcd

Browse files
committed
minor #40047 [HttpClient] Remove unnecessary "?" in url query (michaljusiega)
This PR was merged into the 5.3-dev branch. Discussion ---------- [HttpClient] Remove unnecessary "?" in url query | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | When creating the Request with passing nullable values as query parameters `HttpClientTrait::resolveUrl` method keeps at the end of URL "?" (IMO unnecessary) character. Before: ``` $client = HttpClient::create(); $response = $client->request('GET', 'http://example.com/', ['query' => ['a' => null]]); $url = $response->getInfo('url'); // http://example.com/? ``` After: ``` $client = HttpClient::create(); $response = $client->request('GET', 'http://example.com/', ['query' => ['a' => null]]); $url = $response->getInfo('url'); // http://example.com/ ``` Commits ------- 55831a85db Fix Query URL
2 parents 22cb1a7 + 5da33ad commit 77a9bcd

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

HttpClientTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,10 @@ private static function resolveUrl(array $url, ?array $base, array $queryDefault
436436
$url['path'] = '/';
437437
}
438438

439+
if ('?' === ($url['query'] ?? '')) {
440+
$url['query'] = null;
441+
}
442+
439443
return $url;
440444
}
441445

Tests/HttpClientTraitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public function providePrepareRequestUrl(): iterable
4444
yield ['http://example.com/?a=2&b=b', '.?a=2'];
4545
yield ['http://example.com/?a=3&b=b', '.', ['a' => 3]];
4646
yield ['http://example.com/?a=3&b=b', '.?a=0', ['a' => 3]];
47+
yield ['http://example.com/', 'http://example.com/', ['a' => null]];
48+
yield ['http://example.com/?b=', 'http://example.com/', ['b' => '']];
49+
yield ['http://example.com/?b=', 'http://example.com/', ['a' => null, 'b' => '']];
4750
}
4851

4952
/**

0 commit comments

Comments
 (0)