I am trying to pass my api authtoken via the header. I am new to angular js so i am not able to do that. My code:
$scope.init=function(authtoken,cityname){
$scope.authtoken=authtoken;
$scope.cityname=cityname;
$http({method: 'GET', url: '/api/v1/asas?city='+$scope.cityname+'&auth='+$scope.authtoken}).success(function(data) {
Right now I am passing the authtoken in the api url. But I want to pass the token via the header.
To send a request with the Bearer Token authorization header, you need to make an HTTP request and provide your Bearer Token with the "Authorization: Bearer {token}" header. A Bearer Token is a cryptic string typically generated by the server in response to a login request.
The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Type dropdown list. In the Token field, enter your API key value. For added security, store it in a variable and reference the variable by name.
This type of token is known as a Bearer Token, meaning that it identifies the user that owns it, and defines a user session. A bearer token is a signed temporary replacement for the username/password combination!
usually you pass auth token in headers. Here is how i did it for one of my apps
angular.module('app', []).run(function($http) {
$http.defaults.headers.common.Authorization = token;
});
this will add auth token to headers by default so that you wont have to include is every time you make a request. If you want to include it in every call then it will be something like this
$http({
method: 'GET',
url: '/api/v1/asas?city='+$scope.cityname,
headers:{
'Authorization': $scope.authtoken
}
}).success(function(data) {
//success response.
}).error(function(error){
//failed response.
});
You can configure on application run
youapp.run(function($http) {
$http.defaults.headers.common.Authorization = 'Basic YmVlcDpib29w'
});
or pass it throw each request
$http({
url:'url',
headers:{
Authorization : 'Basic YmVlcDpib29w'
}
})
Angular $Http reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With