I'm trying to modify the working example from deerawan to include header for another website that requires a header. I was given the solution below and it seems to work perfectly. But when I add subscribe(), it still works, but I get exception in console:
What should I do to fix this warning message? Thank you.
Original code:
export class UserService {
private serviceUrl = 'https://jsonplaceholder.typicode.com/users';
constructor(private http: HttpClient) { }
getUser(): Observable<User[]> {
return this.http.get<User[]>(this.serviceUrl);
}
}
Solution code:
export class NWSForecast {
private config = {
headers: {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'
}
};
private serviceUrl = 'https://api.weather.gov/gridpoints/OKX/36,38/forecast';
constructor(private http: HttpClient) { }
getUser(): Observable<any> {
// first argument is URL, put config as second argument
return this.http.get<any>(this.serviceUrl, this.config);
}
}
Modified solution in order to capture response. But it has exception:
export class AppComponent {
private config = {
headers: {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'
}
};
weathers: any;
private serviceUrl = 'https://api.weather.gov/gridpoints/OKX/36,38/forecast';
constructor(private http: HttpClient) { }
getWeather() {
this.http.get<Weather>(this.serviceUrl, this.config).subscribe(
val => {
this.weathers = val;
console.log('this.weather ====> ', this.weathers);
});
}
}
The error message in console:
Refused to set unsafe header "User-Agent" http.js:1436
