Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS How to include header in Http request

Tags:

angularjs

I am new to angularjs. I am trying to make an API request that requires authorization. I have included it in the header of the request, but it is still not working. I am sure my access token is working. Any advice?

$scope.fetch = function() {
    $scope.code = null;
    $scope.response = null;
    $http({
        method: $scope.method,
        url: $scope.url,
        cache: $templateCache,
        headers: {
            Authorization: "access token"
        }
    }).
    success(function(data, status) {
        $scope.status = status;
        $scope.data = data;
    }).
    error(function(data, status) {
        $scope.data = data || "Request failed";
        $scope.status = status;
    });
};
like image 745
Wes Avatar asked Oct 18 '13 21:10

Wes


1 Answers

You could use following

 $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8";

It will add the above header to every POST call you make from your app. For adding a header common to all method, try following.

$http.defaults.headers.common['Authorization'] = "Bearer " + user.oauthInfo.access_token;
like image 178
vs4vijay Avatar answered Oct 13 '22 08:10

vs4vijay