Skip to content

[HttpClient] Inconsistent interface for adding headers with/out file uploads #38082

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

Closed
ThomasLandauer opened this issue Sep 6, 2020 · 3 comments

Comments

@ThomasLandauer
Copy link
Contributor

Description
Usually you add a header like this - see https://symfony.com/doc/current/http_client.html#headers

$response = $client->request('POST', 'https://...', [
    'headers' => [
        'User-Agent' => 'foo',
    ],
]);

But how can you add a header when doing a file upload? https://symfony.com/doc/current/http_client.html#uploading-data doesn't say anything about this, so I'm guessing:

$headers = $formData->getPreparedHeaders()->toArray();
$headers[] = 'User-Agent: foo';

Questions:

  1. Is this really the best way to do it? => If yes, I'd come up with a PR to add this to the docs
  2. Notice the inconsistent syntax: associative array vs. indexed array. So what about a addHeader method that works in all cases?
  3. (somewhat off-topic) It would be nice to have a method to override the config from framework.yaml for the client (not the request). My use case: I'm doing $client-request() multiple times, and I need the same User-Agent for all of them. The string is dynamic (so I can't set it in framework.yaml), but it's the same for every PHP session, so something like $client->setOptions(...) would work.
@ThomasLandauer
Copy link
Contributor Author

ThomasLandauer commented Sep 7, 2020

Another example for 3 would be base_uri.

@nicolas-grekas
Copy link
Member

Is this really the best way to do it? => If yes, I'd come up with a PR to add this to the docs

Yes. all syntaxes are allowed exactly for this: to ease with different styles depending on the situation. Consistency is on the side of the consumer, if it matters.

So what about a addHeader method that works in all cases?

Could be added to the HttpOptions class if you want to have a look.

something like $client->setOptions(...) would work.

there is another way that frees us from having a mutable service (which would be no-go): use decoration.

Something like that:

$configuredClient = new ScopingHttpClient($injectedClient, ['.*' => ['headers' => ['User-Agent' => 'foo']]]);

If this is common enough, we might consider a factory method in ScopingHttpClient?

ThomasLandauer added a commit to ThomasLandauer/symfony-docs that referenced this issue Sep 13, 2020
@nicolas-grekas
Copy link
Member

Closing as explained.

OskarStark added a commit to symfony/symfony-docs that referenced this issue Oct 30, 2020
This PR was merged into the 4.4 branch.

Discussion
----------

Update http_client.rst

Integrating the findings of symfony/symfony#38082 (comment)

Commits
-------

c8e5dd6 Update http_client.rst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants