0

I have the following code to call WebApi from localhost, which working great.

var app = angular.module('myApp', []);
app.controller('employeesController', function ($scope, $http) {
    $http({
        Method: 'GET',
        url: 'http://localhost:50515/api/employees'
    }).then(function (result) {
        $scope.Getemployees = result.data;
    });    
});

i also setup WebApi service which working great tested using postman. But i would like to include WebApi Header information: ApiKey name and apikey value. how can i modify the above script?

url: https://example.api.services/api/XX/v2/employees/all   //url from postman
Key: api-key
value: ba741c13417b7775524dbf0f417cefc4
georgeawg
  • 48,608
  • 13
  • 72
  • 95
Rob
  • 161
  • 2
  • 16
  • You set the headers in your `$http` call. See [AngularJS: API: $http](https://docs.angularjs.org/api/ng/service/$http#setting-http-headers) for a reference. The documentation will also show how to set this on a "global" level so that you only need to set this up in a single place (and not litter your code) – Brendan Green Nov 14 '19 at 06:15

1 Answers1

1

In the headers parameter add an object with a key-value pair.

$http({
    Method: 'GET',
    url: 'http://localhost:50515/api/employees',
    headers: {
        'api-key': 'ba741c13417b7775524dbf0f417cefc4'
    },
}).then(function (result) {
    $scope.Getemployees = result.data;
});
Lemuel Castro
  • 166
  • 11