Question

My backend is structured similar to this article.

So I would like to send GET request with angular.js similar to how curl does it, authorizing via http headers:

$ curl -u miguel:python -i -X GET http://127.0.0.1:5000/api/token
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 139
Server: Werkzeug/0.9.4 Python/2.7.3
Date: Thu, 28 Nov 2013 20:04:15 GMT

{
  "token": "eyJhbGciOiJIUzI1NiIsImV4cCI6MTM4NTY2OTY1NSwiaWF0IjoxMzg1NjY5MDU1fQ.eyJpZCI6MX0.XbOEFJkhjHJ5uRINh2JA1BPzXjSohKYDRT472wGOvjc"
}

Update:

This is not duplicate, answer doesn't describe how to send username and password together in http header, also last comment states that solution doesn't work at all. And please notice not even from my question or that question is possible to understand for random user how to send username:password via http headers.. nobody told anything about base64 yet.. ?:)

Update 2

and full and normal answer IS:

$http.get(config.apiUrl + '/api/token', { headers: {'Authorization': 'Basic '+ base64.encode( $scope.username + ':' + $scope.password) } })

Update 3

This is not easy as I thought, passing this dictionary in .get() won't solve anything. I found this answer helpfull: How do I get basic auth working in angularjs?

Was it helpful?

Solution 2

Http URI allow you to pass also user and password. Try this :

http://miguel:python@127.0.0.1:5000/api/token

OTHER TIPS

To send a GET request to your server, you can use the $http service, and set the headers :

$http.defaults.headers.get = {'X-Access-Token':token};

 $http.get('/api',{params:{}})
      .success(function(response){
         //success
      }).error(function(){
         //error
      });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top