Skip to content

[HttpClient] Content-Type header overridden if body is an array #47852

Closed
@tomme87

Description

@tomme87

Symfony version(s) affected

6.1.6

Description

When sending a request with symfony http-client and the body in the options array is an array, the content-type is always set to application/x-www-form-urlencoded even if I explicitly set another content-type.

I need to set charset on the content type like this Content-Type: application/x-www-form-urlencoded; charset=utf-8, but it gets overridden.

How to reproduce

$response = $this->client->request('POST', '/create', [
  'headers' => [
    'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'
  ],
  'body' => ['key' => 'value']
]);
$response->getContent();

// We can see here that the Content-Type header is "application/x-www-form-urlencoded" and not "application/x-www-form-urlencoded; charset=utf-8"
$debugInfo = $response->getInfo('debug')

Possible Solution

Remove these lines or add a check if the header is already set.

https://github.com/symfony/http-client/blob/c8c887f4813370550147afd27d9eb8a8523e53b2/HttpClientTrait.php#L100-L102

Additional Context

Looks like this was introduced in #45812 but I do not understand the reasoning behind doing it. I should be able to set the header I want. at least be able to set charset.

A workaround for me is to normalize the body into a string my self, but I should not have to do that in my opinion.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions