Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS include API key in a get request

I am trying to get information from a fantasy data API using AngularJS. I am using $resource to perform my get request in my controller, but I haven't been able to figure out how to correctly include the API key. Do I need to include it as a header? Thanks.

nflApp.controller('mainController', ['$scope','$resource','$routeParams', function($scope, $resource, $routeParams) {

$scope.fantasyAPI = $resource("https://api.fantasydata.net/nfl/v2/JSON/DailyFantasyPlayers/2015-DEC-28", { callback: "JSON_CALLBACK" }, { get: { method: "JSONP"}});

console.log($scope.fantasyAPI);

}]);

Below is the http request info from the site. http request info

like image 927
Jeff Tabachnick Avatar asked Dec 28 '15 20:12

Jeff Tabachnick


1 Answers

You should set a header with the API key, AngularJS will send them with every request in the following case:

 $http.defaults.headers.common["Ocp-Apim-Subscription-Key"] = key;

When adding '.common' you are telling angular to send this in every request so you do not need to add it to every resource that hits the API.

like image 193
Lucas Avatar answered Oct 11 '22 06:10

Lucas