15

Can anyone tell me if this is the correct way to add headers to http requests in Angular 6?

When I make the call via SwaggerUI, I can see the headers should be:

url -X GET --header 'Accept: application/json' --header 'zumo-api-version: 2.0.0' 'https://myurl/tables/stuff'

so I have added the following:

let headers: HttpHeaders = new HttpHeaders();
headers = headers.append('HttpHeader1', 'Accept:application/json');
headers = headers.append('HttpHeader2', 'zumo-api-version:2.0.0');

And then the call:

getStuff(){
    return this.http.get('https://myurl/tables/stuff', {headers})
  }

There is no failure but nothing is returned, and I know that there should be.

thanks

UPDATE

Have just noticed that the url in my call is actually https not http, would that make any difference?

getStuff(){
        return this.https.get('https://myurl/tables/stuff', {headers})
      }
2
  • 1
    Where you've got HttpHeader1 should actually be the header name, i.e. Accept, and where you've got Accept:application/json should be the value, i.e. application/json, so what you actually want is headers.append('Accept', 'application/json'); Commented Aug 23, 2018 at 15:40
  • 1
    Make sure you call subscribe on your HTTP call as well. Observables are lazy, so it will only make the HTTP call once something has subscribed Commented Aug 23, 2018 at 15:53

7 Answers 7

32

The correct way to set headers is

let headers: HttpHeaders = new HttpHeaders();
headers = headers.append('Accept', 'application/json');
headers = headers.append('zumo-api-version', '2.0.0');
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, then the call should still be: getStuff(){ return this.http.get('myurl/tables/stuff', {headers}) } ?
sir i tried ur code but i got ERROR TypeError: CreateListFromArrayLike called on non-object in the console can u tell me how to fix?
10

Angular 6 format:

let headers = new HttpHeaders({
    'Accept': 'application/json',
    'zumo-api-version': '2.0.0'
});

2 Comments

try to change 'Accept': 'application/json' to 'Content-Type': 'application/json'
short and simple method to set headers :)
3

The correct format to set the headers would be as shown below.

let headers: HttpHeaders = new HttpHeaders();
headers = headers.append('Accept', 'application/json');
headers = headers.append('zumo-api-version', '2.0.0');

url -X GET --header 'Accept: application/json' --header 'zumo-api-version: 2.0.0' 'https://myurl/tables/stuff'

In the above request the name of the header keys is Accept & zumo-api-version , the text preceding the :
Headers are basically set as key/value pairs

3 Comments

let options = new RequestOptions({ headers: headers }); return this._http .get(this._url,options) make sure you make the call to get request using above statement, by passing the headers in request call
same thing - no data, and yes I get it returned in swagger. Think I'm missing the subscribe though, I'll test that in the morning, home time now.
If it's a GET request, there will be no content, so Content-Type won't matter
1

In angular 6+

declaration zone:

httpOptionsNoAuth : any;

initialization:

constructor(){
    this.httpOptionsNoAuth = {
        headers: new HttpHeaders().set('No-Auth', 'true')
    };
}

usage:

return this._http.get<any>(`${url}`, { headers: this.httpOptionsNoAuth.headers});

Comments

0

You're getting nothing in return because you're not subscribing to that event. add .subcribe to that function where ever you're calling it eg

getStuff().subscribe(data=>{ console.log(data); } )

so the data you're subscribing to contains all the response and everything you need to know about that call.

You can read more from here https://angular.io/guide/http

Comments

0

I have done it like this in my code

httpOptions={ headers: new HttpHeaders({ 'Content-Type': 'application/json'})};
 this.httpOptions.headers = this.httpOptions.headers.append('Token', this.Token);

And then in my http.get call, I have done this:

return this.http.get<JSON>(this.API_ADDRESS+'/api/RemoveEmployee/'+id,this.httpOptions

Comments

-3

Try below code that might help you.

let headers = new HttpHeaders().set('Accept': 'application/json').set('zumo-api-version': '2.0.0')

4 Comments

At least learn to format your OWN codes before trying to help others.
@JhourladEstrella thank you for advice. I will take care of next time.
Downvoted, because the syntax for adding a header is inaccurate. The correct method is append, not set.
I give this for Angular 6. [angular.io/api/common/http/HttpHeaders]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.