0

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'
   ]);
}
0

2 Answers 2

3

You can use withHeaders again to merge your headers conditionally with the previous headers as shown in the Laravel 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.

<?php

$httpRequest = Http::baseUrl(config('api_base_url'))
               ->withHeaders([
                'Authorization' => 'Bearer ' . $accessToken,
               ]);


if ($var === 'me') {
   $httpRequest->withHeaders([
       "Content-Type" => 'multipart/form-data'
   ]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Make header content as array variable. For example

if($var === "me") {
     $headers = [
        'Authorization' => 'Bearer ' . $accessToken,
        "Content-Type" => 'multipart/form-data'
     ];
} else {
     $headers = [
        'Authorization' => 'Bearer ' . $accessToken
     ];
}


$httpRequest = Http::baseUrl(config('api_base_url'))
->withHeaders($headers);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.