Skip to content

[HttpClient] Copy as curl fixes #46583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2022

Conversation

HypeMC
Copy link
Contributor

@HypeMC HypeMC commented Jun 4, 2022

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:
    $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:
    $httpClient->request('POST', 'https://symfony.com', [
        'body' => [
            'bar' => 'newbarval',
            'foobar' => [
                'baz' => 'bazval',
                'qux' => 'quxval',
            ],
            'bazqux' => ['bazquxval1', 'bazquxval2'],
        ],
    ]);
    Warning: Array to string conversion
    

@carsonbot
Copy link

Hey!

I think @Deuchnord has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@nicolas-grekas
Copy link
Member

Thank you @HypeMC.

@nicolas-grekas nicolas-grekas merged commit 72ab165 into symfony:6.1 Jun 9, 2022
@HypeMC HypeMC deleted the copy-as-curl-fixes branch June 9, 2022 12:59
@fabpot fabpot mentioned this pull request Jun 9, 2022
@trsteel88
Copy link
Contributor

Fyi, I have noticed that if you use "Copy as curl" when a base URL is used, the copy only contains the relative URL.

e.g.

$this->httpClient = $httpClient->withOptions([
            'base_uri' => 'https://domain.com/api/',
            'headers' => [
                'Content-Type' => 'application/json',
                'Api-Key' => $apiKey,
             ],
        ]);

this->httpClient->request('GET', 'foo/bar');

results in:

curl \
  --compressed \
  --request GET \
  --url 'foo/bar' \
  --header 'content-type: application/json' \
  --header 'api-key: ABC123' \
  --header 'accept: */*' \
  --header 'user-agent: Symfony HttpClient/Curl' \
  --header 'accept-encoding: gzip'

It should result in:

curl \
  --compressed \
  --request GET \
  --url 'https://domain.com/api/foo/bar' \
  --header 'content-type: application/json' \
  --header 'api-key: ABC123' \
  --header 'accept: */*' \
  --header 'user-agent: Symfony HttpClient/Curl' \
  --header 'accept-encoding: gzip'

nicolas-grekas added a commit that referenced this pull request Jun 19, 2022
This PR was merged into the 6.1 branch.

Discussion
----------

[HttpClient] Fix Copy as curl with base uri

| Q             | A
| ------------- | ---
| Branch?       | 6.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #46583 (comment)
| License       | MIT
| Doc PR        | -

Fixes Copy as curl when `base_uri` is used.

Commits
-------

12e40a0 [HttpClient] Fix Copy as curl with base uri
symfony-splitter pushed a commit to symfony/http-client that referenced this pull request Jun 19, 2022
This PR was merged into the 6.1 branch.

Discussion
----------

[HttpClient] Fix Copy as curl with base uri

| Q             | A
| ------------- | ---
| Branch?       | 6.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix symfony/symfony#46583 (comment)
| License       | MIT
| Doc PR        | -

Fixes Copy as curl when `base_uri` is used.

Commits
-------

12e40a02b1 [HttpClient] Fix Copy as curl with base uri
sadafrangian3 pushed a commit to sadafrangian3/Dependency-Injection-http-client that referenced this pull request Nov 2, 2022
This PR was merged into the 6.1 branch.

Discussion
----------

[HttpClient] Fix Copy as curl with base uri

| Q             | A
| ------------- | ---
| Branch?       | 6.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix symfony/symfony#46583 (comment)
| License       | MIT
| Doc PR        | -

Fixes Copy as curl when `base_uri` is used.

Commits
-------

12e40a02b1 [HttpClient] Fix Copy as curl with base uri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants