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})
}
To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers.
In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name. In the Value box, type the custom HTTP header value.
To add headers for an HTTP method other than POST or PUT, simply add a new object with the lowercased HTTP method name as the key, e.g. $httpProvider. defaults. headers. get = { 'My-Header' : 'value' } .
What Is HttpClient? HttpClient is a built-in service class available in the @angular/common/http package. It has multiple signature and return types for each request. It uses the RxJS observable-based APIs, which means it returns the observable and what we need to subscribe it.
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');
Angular 6 format:
let headers = new HttpHeaders({
'Accept': 'application/json',
'zumo-api-version': '2.0.0'
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With