0

I have a factory with a number of functions that look like this,

doFunction: function () {
   return $http({
       method: 'POST',
       url: 'https://localhost:8000/test',
       data: {},
       headers: {
          "My_Header": $rootScope.value,
          "Content-Type": "application/json; charset=utf-8"
       }
    })
}

I want to add a custom header called My_Header, but I dont think this is the correct way of doing it since it is not included on the request. What am I doing wrong here?

R. Richards
  • 24,603
  • 10
  • 64
  • 64
dearn44
  • 3,198
  • 4
  • 30
  • 63
  • What if you change the name to "My-Header" (or MyHeader)? I couldn't find the required format for header names, but if it is actually not showing up as a request header as you say, then it could be because chrome doesn't like the look of it, although I couldn't find any reason why your code shouldn't work. – Adam Jenkins Mar 02 '19 at 20:09

1 Answers1

0

Create a new HttpHeaders() from here.

Then add your header: .append('My_Header', $rootScope.value).

Now use these new headers for your request.

tehp
  • 5,018
  • 1
  • 24
  • 31
  • 1
    Odd this was the accepted answer seeing how [angularjs docs](https://docs.angularjs.org/api/ng/service/$http#setting-http-headers) let you pass in a plain object – Adam Jenkins Mar 02 '19 at 20:14
  • `HttpHeaders` is an Angular 2+ class. Why would adding it to AngularJS solve the problem? – georgeawg Mar 02 '19 at 21:43