I have some concerns(marigolds) with my method GET when I send my request. I have received an error 401 and I am disconnected from the application, knowing that the token which I obtain in the console is very valid when I test on postman. I does not really understand(include) why that does not work I have to add some things or I have pain make something.
var list = function(){
var mytoken = window.localStorage.getItem('token');
return $http({
method : 'GET',
url : apiEndpoint.url + '/list',
withCredentials : true,
headers : {Authorization : 'Bearer ' + mytoken}
}).then(function(res) {
console.log(res.data);
});
};
If somebody could me helped please, thank you :)
I think that she uses of the angularJS in her project, while RequestOptions is a class which we find that in Angular 2. That's why the import does not work
It must be added in the conf of apache:
SetEnvIf Authorization. + HTTP_AUTHORIZATION = $ 0
It should work for your case :)
Please check it like below:
let headers = new Headers();
headers.append('Authorization', 'Bearer ' + YourToken);
let options = new RequestOptions({ headers: headers });
this.http.get('YourUrl', options)
This should work
Related
I am very new to angularJS.
My Backend is DRF and I have successfully implemented token.
this is my token:
{
"key": "217c3b5913b583a0dc3285e3521c58b4d7d88ce2"
}
Before I implement token in backend, it was working nice:
$scope.getAllContact = function() {
var data = $http.get("http://127.0.0.1:8000/api/v1/contact")
.then(function(response) {
$scope.contacts = response.data;
});
};
But, now I am not getting how can I implement this token here
Can anyone help me in this case?
Try to use this. You need to attach the token in the headers.
$http({
url : "http://127.0.0.1:8000/api/v1/contact",
method : 'GET',
headers : {
'Content-Type' : 'application/json',
'key': "217c3b5913b583a0dc3285e3521c58b4d7d88ce2"
}
}).then(function(response){
$scope.contacts = response.data;
});
Note that, this is binding the token to only this request. Use $http interceptors to add the token to each request that you make.
See here: Angular Js - set token on header default
trying to call data through openweathermap api
if I call it through 'GET'method.there is
405 (Method Not Allowed)
var req = {
method: 'GET',
url: 'http://api.openweathermap.org/data/2.5/forecast/daily?APPID=' + ApiKey + '&q=London,us',
headers: {
'x-api-key': ApiKey
}
}
$http(req)
.then(function (data) {
console.log(data);
}, function (err) {
console.log(err);
});
#faisal
I ran into this error today, and after some debugging, I realized that it was because I had $httpProvider.defaults.headers.common['X-Requested-With'] = 'HttpRequest'; in my config file. I just disabled CORS for convenience and solved the problem.
This answer is what helped me: Disable CORS in angularJS
Use the params property to encode the search parameters:
var req = {
method: 'GET',
//url: 'http://api.openweathermap.org/data/2.5/forecast/daily?APPID=' + ApiKey + '&q=London,us',
url: 'http://api.openweathermap.org/data/2.5/forecast/daily',
params: { appid: ApiKey, q: 'London,us' }
}
The problem is likely that the &q=London,us is illegal. The comma character needs to be percent-encoded to q=London%2Cus. Use the params property to properly encode the parameters.
Update
I tested it with my APPID (which I am not going to publish) and it worked.
Here is my DEMO on PLNKR with the APPID removed.
I am developing a app in Ionic framework.The api calls data from secured server.When i check the api in browser i am getting this popup.
So i could not use this api in mobile app.i found some solution for this issue nothing worked Here is the code i have tried
var string = username + ":" + password;
// Encode the String
var encodedString = btoa(string);
$http({method: 'GET', url: 'https://creatorup.com/wp-json/users/me',
headers: { 'Authorization': 'Basic ' + encodedString }
})
When i use this code i got the response "401 (unauthorized)" error.
Whenever i fire this put method :
return $http.put('/posts/' + post._id + '/upvote', {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function(data){
post.upvotes += 1;
});
I get the following error :
PUT http://localhost:3000/posts/56d89bfbb906ee1305fea2cf/upvote 401 (Unauthorized)
From what I understand, this is an angular issue with the way that its put method supports sending data to the server.
Can anyone who has dealt with this issue before help me out by.
-Thank you very much in advance
Try setting the HTTP header before hand:
$http.defaults.headers.common.Authorization = 'Bearer '+ auth.getToken();
Then call your PUT request
Angular v1.3.5
I'm trying to pass serialized data to my API. It requires the Content-Type header to be application/x-www-form-urlencoded; charset=UTF-8;
For POST, I've set this up as follows in my .run:
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8;";
This works great for POST. However, for PUT doing the same is completely ignored.
$http.defaults.headers.put["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8;";
I even tried putting this in the request. The Content-Type was still ignored.
$http({
method : 'PUT',
url : SMARTWORX_CONFIGS.APIURL + 'users/' + service.profile.id + '.json',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8;'
},
data : {
user : profile
}
})
I've been forced to solve this by using an interceptor and adding the headers to the config object there. It's quite a hack, but it works.
Am I doing something wrong here?
After beating my head on the wall some more, I discovered the problem - of my own making.
My app is using tokens for authentication; so, I have an interceptor to inject the token into the headers when needed. I made a mistake with this.
It looked like :
config.headers = config.header || {};
config.headers['X-AUTH-TOKEN'] = result;
It SHOULD have been written like:
config.headers = config.headers || {};
config.headers['X-AUTH-TOKEN'] = result;
Basically, I was blowing out all previous headers.