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"? 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. Sep 2, 2015 at 2:03
  • possible duplicate of Set HTTP header for one request 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)
  });
3
  • Sorry for my question but doesn't this approach expose your API key (using developer tools)?
    – Sohlae
    Aug 2, 2017 at 15:00
  • Yes, it would. Depending on your use case, that may or may not be acceptable. Aug 6, 2017 at 18:20
  • @AnidMonsur could you elaborate on why this is exposing the token? Is that still true if one uses TLS? Mar 13, 2019 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.