0

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?

Community
  • 1
  • 1
holms
  • 9,112
  • 14
  • 65
  • 95
  • Where is the username and password? You have a JWT, but you would have this _after_ logging in. Are you asking simply how to add a header to an angular HTTP request? – Davin Tryon May 15 '14 at 09:33
  • @holms, please outline the problem you're having. Do not expect SO users to read through the article you linked to in order to (maybe) understand what your issue is. What are you trying to achieve exactly, what's the obstacle you're facing, what have you tried so far? – mingos May 15 '14 at 09:36
  • I think that's exactly what he need, the question also have Basic Auth. – jcubic May 15 '14 at 09:41
  • btw it's not duplicate, in that question nothing is told how to send USERNAME and PASSWORD together, only token.. or whatever crap is in there. and last comment states that solution doesn't work at all – holms May 15 '14 at 10:14
  • @holms you don't sending token the token is returned by the request, you're seding user and password in curl `-u miguel:python` that's converted to base64 in header by curl. – jcubic May 15 '14 at 10:17
  • so angular.js can't handle auth headers with base64 in one go? – holms May 15 '14 at 10:18
  • 1
    @holms basic auth is created using header `Authorization: Basic Base64('user:password')` https://en.wikipedia.org/wiki/Basic_access_authentication – jcubic May 15 '14 at 10:25
  • so should I apply base64 manually or not? `.get(config.apiUrl + '/api/token', { headers: {'Authorization': 'Basic '+$scope.username+':'+$scope.password} })` :) – holms May 15 '14 at 10:30
  • ok solved: `base64.encode()` – holms May 15 '14 at 11:14

2 Answers2

2

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
      });
Riron
  • 1,023
  • 14
  • 18
  • Not answering a question for me, how to send basic http auth? But thanks for mentioning `X-Access-Token`, because this will be used within every next request! :) – holms May 15 '14 at 09:54
1

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

http://miguel:python@127.0.0.1:5000/api/token
  • I think it's only handled by the browsers, because user/password need to be send in header with base64, but not 100% sure. – jcubic May 15 '14 at 09:42
  • elegant solution! :D how can I forget.. – holms May 15 '14 at 09:48
  • @jcubic i wonder if this gonna work if password will have @ and : characters? :) anyway to escape them? – holms May 15 '14 at 10:08
  • @holms try solution from the answer that I've mark as duplicate. It use base64 inside a header. That's how basic Auth is handled by the browser. – jcubic May 15 '14 at 10:15
  • as last comment user states in there, solution in selected answer doesn't work, for me actually is the same.. – holms May 15 '14 at 10:17