Home > front end >  Add an API Key to Http request in laravel 8
Add an API Key to Http request in laravel 8

Time:07-27

Below is a POST method that will generate an API Key

Controller:

public function index(Request $request)
{
    $response = Http::post('ENDPOINT', [
        'Username' => 'ADMIN',
        'Password' => 'ADMIN',
        'Token' => 'TEF53...',
    ]);

And the next method (POST) will create new data and use the above response as an API Key

    $response2 = Http::withHeaders([
        'Accept' => 'application/json',
    ])->post('ENDPOINT', [
        'body' => [
            "DocKey" => 11223333355,
            ...
        ]
    ]);
    return json_decode($response2);
}

But in this case i am getting an error:

Message "Authorization has been denied for this request."

The reason is the API Key isn't provided in the previous POST method

where can i place the generated API Key from the first method in the second method?


More clarification:

When i test the second method ($response2) in Postman, I need to provide the generated API Key from the first method ($response) as shown below:

https://i.stack.imgur.com/TOp1b.png

How can i add the above (Type API Key and its parameters)?

CodePudding user response:

Looks like

$response2 = Http::withHeaders([
        'Accept' => 'application/json',
        'Authorization' => 'eyJ...', // provide apiKey here
])->post...
  • Related