Search code examples
phplaravelheaderauthorizationunirest

php how to pass authorization bearer in unirest


i have unirest code running in laravel 4.2: (doesn't work)

<?php
    $headers = array('Authorization', 'Bearer TOKENASDKASKDN231DAS2');
    $body    = array();
    $respons = Unirest\Request::get("https://api.request", $headers, $body);
?>
// and this
<?php
  Unirest\Request::auth('TOKENASDKASKDN231DAS2', '');
  $header  = array();
  $body    = array();
  $respons = Unirest\Request::get("https://api.request", $headers, $body);
?>

I tried running this in getpostman

URL: GET - https://api.request

HEADER: Authorization : Bearer TOKENASDKASKDN231DAS2

It actually works. Don't why not in unirest.


I already have a working code with using the Auth Basic:

Authorization: Basic c2tfdGVzdF9uNTA0OWFhNjA1M2M5YTAyMTdiZWE3ODA3MGZiZjUwMTo=

in php:

Unirest\Request::auth('c2tfdGVzdF9uNTA0OWFhNjA1M2M5YTAyMTdiZWE3ODA3MGZiZjUwMTo=', '');


Solution

  • This one is simple.

    This is correct, as you said: Unirest\Request::auth('TOKEN HERE', '');

    This is not $headers = array('Authorization', 'Bearer TOKENASDKASKDN231DAS2');

    Because $headers is getting an array,

    it should be array('Authorization' => 'Bearer TOKENHERE');

    DO NOT COMPARE HEADER and AUTH