I want to add some headers but can’t find it on the docs
$httpRequest = Http::baseUrl(config('api_base_url'))
->withHeaders([
'Authorization' => 'Bearer ' . $accessToken,
]);
// can i do something like this?
if ($var === 'me') {
$httpRequest->pushToExistingHeaders([
"Content-Type" => 'multipart/form-data'
]);
}
2
Answers
You can use
withHeaders
again to merge your headers conditionally with the previous headers as shown in theLaravel code
. This is because it adds to the previous headers and doesn’t replace them.This is how other methods like
accept
,content-type
are accomplishing this.Make header content as array variable. For example