Skip to content

[HTTP Client] "withHeaders" method do not overwrite previous headers. #45059

Closed
@juanparati

Description

@juanparati
  • Laravel Version: 9.26.0 and 9.36.1
  • PHP Version: 8.1.10
  • Database Driver & Version: None

Description:

I observed that when "withHeaders" method is called twice passing the same header, the HTTP Client appends the header value. Despite that that line folding was supported in the old RFC 2616, is obsolete according to RFC 7230. That is means that the right behavior when headers are added is to overwrite the value instead of append it because each header should use one line.

Steps To Reproduce:

use Illuminate\Support\Facades\Http;

$myClient = Http::retry(2);

$myClient->withHeaders(['X-Token' => 'foo']);           // X-Token = foo
$myClient->post('https://example.net/foobar');

$myClient->withHeaders(['X-Token' => 'bar']);           // X-Token = [foo, bar]
$myClient->post('https://example.net/foobar');

Explanation:

The method "withHeaders" use "array_merge_recursive" in order to merge the headers, the method "array_merge_recursive" it appends the elements that already exists instead of overwrite the values. Replacing "array_merge" by "array_merge_recursive" can fix the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions