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.

3
  • Why are you using Token[token]=1111 in Angular and not Authorization: Token token="1111"? Commented Sep 2, 2015 at 1: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. Commented Sep 2, 2015 at 2:03
  • possible duplicate of Set HTTP header for one request Commented Sep 2, 2015 at 2:11

1 Answer 1

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)
  });
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry for my question but doesn't this approach expose your API key (using developer tools)?
Yes, it would. Depending on your use case, that may or may not be acceptable.
@AnidMonsur could you elaborate on why this is exposing the token? Is that still true if one uses TLS?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.