Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guzzle HTTP - add Authorization header directly into request

Can anyone explain how to add the Authorization Header within Guzzle? I can see the code below works for adding the username & password but in my instance I just want to add the Authorization header itself

$client->request('GET', '/get', ['auth' => ['username', 'password'] 

The Basic Authorization header I want to add to my GET request :-

Basic aGdkZQ1vOjBmNmFmYzdhMjhiMjcwZmE4YjEwOTQwMjc2NGQ3NDgxM2JhMjJkZjZlM2JlMzU5MTVlNGRkMTVlMGJlMWFiYmI= 
like image 228
Zabs Avatar asked Mar 15 '16 12:03

Zabs


People also ask

How do I send Authorization header in guzzle?

If you are just looking to add the Authorization header, that should be pretty easy. // Set various headers on a request $client->request('GET', '/get', [ 'headers' => [ 'Authorization' => 'PUT WHATEVER YOU WANT HERE' ] ]);

How do I pass the Authorization header in GET request?

To send a GET request with a Bearer Token authorization header, you need to make an HTTP GET request and provide your Bearer Token with the Authorization: Bearer {token} HTTP header.

How do I send the Authorization header in HTTP?

It is a simple authentication scheme built into the HTTP protocol. The client sends HTTP requests with the Authorization header that contains the word Basic, followed by a space and a base64-encoded(non-encrypted) string username: password. For example, to authorize as username / Pa$$w0rd the client would send.

Is Authorization header automatically sent?

The HTTP Authorization request header can be used to provide credentials that authenticate a user agent with a server, allowing access to a protected resource. The Authorization header is usually, but not always, sent after the user agent first attempts to request a protected resource without credentials.


2 Answers

I don't know how I missed reading that you were looking for the Basic auth header, but nonetheless hope this helps somewhat. If you are just looking to add the Authorization header, that should be pretty easy.

// Set various headers on a request $client->request('GET', '/get', [ 'headers' => [     'Authorization'     => 'PUT WHATEVER YOU WANT HERE'     ] ]); 

I build up my request in Guzzle piece by piece so I use the following:

$client = new GuzzleHttp\Client(); $request = $client->createRequest('GET', '/get'); $request->addHeader('X-Authorization', 'OAuth realm=<OAUTH STUFF HERE>'); $resp = $client->send($request); 

Hope that helps. Also, make sure to include the version of Libraries you are using in the future as syntax changes depending on your version.

like image 68
Matt D. Avatar answered Sep 17 '22 12:09

Matt D.


I'm using Guzzle 6. If you want to use the Basic Auth Scheme:

$client = new Client(); $credentials = base64_encode('username:password'); $response = $client->get('url',         [             'headers' => [                 'Authorization' => 'Basic ' . $credentials,             ],         ]); 
like image 33
Agu Dondo Avatar answered Sep 18 '22 12:09

Agu Dondo