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?

9
  • 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? Commented May 15, 2014 at 9: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? Commented May 15, 2014 at 9:36
  • I think that's exactly what he need, the question also have Basic Auth. Commented May 15, 2014 at 9: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 Commented May 15, 2014 at 10:14
  • 1
    @holms basic auth is created using header Authorization: Basic Base64('user:password') en.wikipedia.org/wiki/Basic_access_authentication Commented May 15, 2014 at 10:25

2 Answers 2

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

1 Comment

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! :)
1

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

http://miguel:[email protected]:5000/api/token

5 Comments

I think it's only handled by the browsers, because user/password need to be send in header with base64, but not 100% sure.
elegant solution! :D how can I forget..
@jcubic i wonder if this gonna work if password will have @ and : characters? :) anyway to escape them?
@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.
as last comment user states in there, solution in selected answer doesn't work, for me actually is the same..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.