Skip to content
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

Override request header in Http Client #47313

Closed
DJafari opened this issue Jun 1, 2023 · 8 comments · Fixed by #47335
Closed

Override request header in Http Client #47313

DJafari opened this issue Jun 1, 2023 · 8 comments · Fixed by #47335

Comments

@DJafari
Copy link

DJafari commented Jun 1, 2023

Laravel Version

10.12.0

PHP Version

8.2.6

Database Driver & Version

No response

Description

in some case if i want change request header, caused dupplicated and only first value passed

check reproduced code and result

Steps To Reproduce

$request = Http::withHeaders([
    'X' => 'AAA'
]);
$request->withHeaders([
    'X' => 'BBB'
]);
dd($request->getOptions());

Result :

array:4 [▼ // routes/web.php:28
  "connect_timeout" => 10
  "http_errors" => false
  "timeout" => 30
  "headers" => array:1 [▼
    "X" => array:2 [▼
      0 => "AAA"
      1 => "BBB"
    ]
  ]
]
@joelbutcher
Copy link
Contributor

joelbutcher commented Jun 1, 2023

@DJafari is this resolved if you use arrays?

request = Http::withHeaders([
    'X' => ['AAA']
]);
$request->withHeaders([
    'X' => ['BBB']
]);

dd($request->getOptions());

@DJafari
Copy link
Author

DJafari commented Jun 1, 2023

@joelbutcher No, for me the output is still the same.

@timacdonald
Copy link
Member

Going to close this, as this is a feature request rather than a bug. Both the first and second values for a header are passed, which the spec supports. The consuming client would need to decide how to handle things.

The following app:

Route::get('/', function () {
    return Http::withHeaders([
        'X-Foo' => 'first',
    ])->withHeaders([
        'X-Foo' => 'second',
    ])->get(url('/dump-headers'))->body();
});

Route::get('/dump-headers', fn () => request()->headers);

Correctly merges and sends both headers. This is the browser output from that application.

Content-Length: 
Content-Type:   
Host:           play.test
User-Agent:     GuzzleHttp/7
X-Foo:          first, second

@timacdonald
Copy link
Member

timacdonald commented Jun 1, 2023

To replace a header, you may call the header function...

Edited: incorrect.

@DJafari
Copy link
Author

DJafari commented Jun 2, 2023

@timacdonald

Method Illuminate\Http\Client\PendingRequest::header does not exist.

PendingRequest doesn't have header function to replace header

so if sending array values is valid, telescope only log first value and and it has bug

image

i think it's bug, if someone want sending array value, he will pass in an array from the very beginning, it's mean only if we pass header with this syntax :

Http::withHeaders([
    'X' => ['AAA']
])->withHeaders([
    'X' => ['BBB']
]);

this result is correct :

array:4 [▼ // routes/web.php:28
  "connect_timeout" => 10
  "http_errors" => false
  "timeout" => 30
  "headers" => array:1 [▼
    "X" => array:2 [▼
      0 => "AAA"
      1 => "BBB"
    ]
  ]
]

@timacdonald
Copy link
Member

@DJafari see #47335

@timacdonald
Copy link
Member

See also laravel/telescope#1355

@DJafari
Copy link
Author

DJafari commented Jun 2, 2023

@timacdonald thanks!

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

Successfully merging a pull request may close this issue.

3 participants