6

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.

syafiq faiz
  • 178
  • 1
  • 2
  • 12
  • Why are you using `Token[token]=1111` in Angular and not `Authorization: Token token="1111"`? – Anid Monsur Sep 02 '15 at 01:57
  • That is my question actually, how to properly write the header. I tried this as well, `$http.get('http://localhost:3000/posts.json', { headers: {"Authorization: Token token=1111"})` but still not working. – syafiq faiz Sep 02 '15 at 02:03
  • possible duplicate of [Set HTTP header for one request](http://stackoverflow.com/questions/11876777/set-http-header-for-one-request) – sulaiman sudirman Sep 02 '15 at 02:11

1 Answers1

11

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)
  });
Anid Monsur
  • 4,538
  • 1
  • 17
  • 24