0

I m trying to call a web service from Angular. I m passing the headers as HttpHeaders object, but I do not see that sent when I check the Network console.

This other post Angular HttpClient doesn't send header seems to have answered it, but I am not able to get that working with the solution that is suggested in it . My call looks like this.

1

 this.http.get(urlString, {headers: new HttpHeaders({'Content-Type': 'application/json',
            'header1': 'value1',
            'header2': 'value2'
           })});

2

let myHeaders = new HttpHeaders();
    myHeaders = myHeaders.set( 'Content-Type', 'application/json')
    .set(  'header1', 'value1')
    .set(  'header2', 'value2')
    ;

this.http.get(urlString, {headers: myHeaders});

3

const httpOptions = {
  headers: new HttpHeaders(
    { 'Content-Type': 'application/json',
              'header1': 'value1',
              'header2': 'value2'
    })
};

this.http.get(urlString, httpOptions);

In all cases, the Network console shows this

Access-Control-Request-Headers:content-type,header1,header2

Is there some mistake which I am making when I declare or set it? Sorry if this question is repetition of others, but the answers were not working in this code. Thank you.

user1592521
  • 61
  • 1
  • 9

1 Answers1

0
Use Headers instead of HttpHeaders

const headers = new Headers({'Content-Type' : 'application/json','header1':'value1','header2':'value2'})

return this.http.get('url', {headers: headers});
sonu singhal
  • 211
  • 1
  • 6
  • the import has import { HttpClient, HttpHeaders } from '@angular/common/http'; the httpclient in it accepts only httpheaders. – user1592521 Feb 06 '18 at 02:13
  • i cant pass headers in this httpclient, it allows only httpheaders. is there any other suggestion? – user1592521 Feb 06 '18 at 02:25