Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular $http GET request with authorization token header

I am trying to replicate this curl using angular $http.get,

curl -H "Accept: application/json" http://localhost:3000/posts -H 'Authorization: Token token="1111"'

but I am not sure how to properly set the angular header

this is how I tried it,

    app.controller('newsCtrl', function($http, $scope){
      $scope.news =[];
      $http.get('http://localhost:3000/posts.json', {
        headers: {"Authorization": "Token[token]=1111"}).success(function(response){
        console.log(response)
      })
    })

So how do I properly set the header?

Thank you ps: I am not using Basic authentication.

like image 496
syafiq faiz Avatar asked Sep 02 '15 01:09

syafiq faiz


People also ask

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 can I get Bearer Token from Authorization header?

The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Type dropdown list. In the Token field, enter your API key value. For added security, store it in a variable and reference the variable by name.


1 Answers

If you want the Authorization header to contain Token token="1111", then this should work. It looks like your brackets were not matching up also.

  $http.get('http://localhost:3000/posts.json', {
    headers: {
        "Authorization": 'Token token="1111"'
    }
  }).success(function(response){
    console.log(response)
  });
like image 175
Anid Monsur Avatar answered Sep 30 '22 21:09

Anid Monsur