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 72ab165

Browse filesBrowse files
bug #46583 [HttpClient] Copy as curl fixes (HypeMC)
This PR was squashed before being merged into the 6.1 branch. Discussion ---------- [HttpClient] Copy as curl fixes | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - The "Copy as curl" button doesn't work as expected in some cases: 1) it ignores the `query` option: ```php $httpClient->request('GET', 'https://symfony.com?foo=fooval&bar=barval', [ 'query' => [ 'bar' => 'newbarval', 'foobar' => [ 'baz' => 'bazval', 'qux' => 'quxval', ], ], ]); ``` ``` curl \ --compressed \ --request GET \ --url 'https://symfony.com?foo=fooval&bar=barval' \ --header 'accept: */*' \ --header 'user-agent: Symfony HttpClient/Curl' \ --header 'accept-encoding: gzip' ``` 2) it fails if the body is a multidimensional array or object: ```php $httpClient->request('POST', 'https://symfony.com', [ 'body' => [ 'bar' => 'newbarval', 'foobar' => [ 'baz' => 'bazval', 'qux' => 'quxval', ], 'bazqux' => ['bazquxval1', 'bazquxval2'], ], ]); ``` ``` Warning: Array to string conversion ``` Commits ------- 0bc7caf [HttpClient] Copy as curl fixes
2 parents 3184226 + 0bc7caf commit 72ab165
Copy full SHA for 72ab165

File tree

Expand file treeCollapse file tree

2 files changed

+48
-6
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+48
-6
lines changed

‎src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpClient\DataCollector;
1313

14+
use Symfony\Component\HttpClient\HttpClientTrait;
1415
use Symfony\Component\HttpClient\TraceableHttpClient;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpFoundation\Response;
@@ -23,6 +24,8 @@
2324
*/
2425
final class HttpClientDataCollector extends DataCollector implements LateDataCollectorInterface
2526
{
27+
use HttpClientTrait;
28+
2629
/**
2730
* @var TraceableHttpClient[]
2831
*/
@@ -176,7 +179,7 @@ private function getCurlCommand(array $trace): ?string
176179
}
177180

178181
$debug = explode("\n", $trace['info']['debug']);
179-
$url = $trace['url'];
182+
$url = self::mergeQueryString($trace['url'], $trace['options']['query'] ?? [], true);
180183
$command = ['curl', '--compressed'];
181184

182185
if (isset($trace['options']['resolve'])) {
@@ -196,8 +199,9 @@ private function getCurlCommand(array $trace): ?string
196199
if (\is_string($body)) {
197200
$dataArg[] = '--data '.escapeshellarg($body);
198201
} elseif (\is_array($body)) {
199-
foreach ($body as $key => $value) {
200-
$dataArg[] = '--data '.escapeshellarg("$key=$value");
202+
$body = explode('&', self::normalizeBody($body));
203+
foreach ($body as $value) {
204+
$dataArg[] = '--data '.escapeshellarg(urldecode($value));
201205
}
202206
} else {
203207
return null;

‎src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php
+41-3Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,21 @@ public function provideCurlRequests(): iterable
244244
'foo' => 'fooval',
245245
'bar' => 'barval',
246246
'baz' => 'bazval',
247+
'foobar' => [
248+
'baz' => 'bazval',
249+
'qux' => 'quxval',
250+
],
251+
'bazqux' => ['bazquxval1', 'bazquxval2'],
252+
'object' => (object) [
253+
'fooprop' => 'foopropval',
254+
'barprop' => 'barpropval',
255+
],
256+
'tostring' => new class() {
257+
public function __toString(): string
258+
{
259+
return 'tostringval';
260+
}
261+
},
247262
],
248263
],
249264
],
@@ -253,14 +268,37 @@ public function provideCurlRequests(): iterable
253268
--url %1$shttp://localhost:8057/json%1$s \\
254269
--header %1$sAccept: */*%1$s \\
255270
--header %1$sContent-Type: application/x-www-form-urlencoded%1$s \\
256-
--header %1$sContent-Length: 32%1$s \\
271+
--header %1$sContent-Length: 211%1$s \\
257272
--header %1$sAccept-Encoding: gzip%1$s \\
258273
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s \\
259-
--data %1$sfoo=fooval%1$s --data %1$sbar=barval%1$s --data %1$sbaz=bazval%1$s',
274+
--data %1$sfoo=fooval%1$s --data %1$sbar=barval%1$s --data %1$sbaz=bazval%1$s --data %1$sfoobar[baz]=bazval%1$s --data %1$sfoobar[qux]=quxval%1$s --data %1$sbazqux[0]=bazquxval1%1$s --data %1$sbazqux[1]=bazquxval2%1$s --data %1$sobject[fooprop]=foopropval%1$s --data %1$sobject[barprop]=barpropval%1$s --data %1$stostring=tostringval%1$s',
260275
];
261276

262-
// escapeshellarg on Windows replaces double quotes with spaces
277+
// escapeshellarg on Windows replaces double quotes & percent signs with spaces
263278
if ('\\' !== \DIRECTORY_SEPARATOR) {
279+
yield 'GET with query' => [
280+
[
281+
'method' => 'GET',
282+
'url' => 'http://localhost:8057/?foo=fooval&bar=barval',
283+
'options' => [
284+
'query' => [
285+
'bar' => 'newbarval',
286+
'foobar' => [
287+
'baz' => 'bazval',
288+
'qux' => 'quxval',
289+
],
290+
'bazqux' => ['bazquxval1', 'bazquxval2'],
291+
],
292+
],
293+
],
294+
'curl \\
295+
--compressed \\
296+
--request GET \\
297+
--url %1$shttp://localhost:8057/?foo=fooval&bar=newbarval&foobar%%5Bbaz%%5D=bazval&foobar%%5Bqux%%5D=quxval&bazqux%%5B0%%5D=bazquxval1&bazqux%%5B1%%5D=bazquxval2%1$s \\
298+
--header %1$sAccept: */*%1$s \\
299+
--header %1$sAccept-Encoding: gzip%1$s \\
300+
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s',
301+
];
264302
yield 'POST with json' => [
265303
[
266304
'method' => 'POST',

0 commit comments

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