I want to read the StackOverflow job RSS feed, I tried adding "Access-Control-Allow-Headers" and proxy implementation as well, it errors out. Please help me to achieve this [SOLVED, Please see the answer below]
API URL: https://stackoverflow.com/jobs/feed
private proxyPrefix = '/api';
private hostName = 'stackoverflow.com';
private location='sydney'
private query = `location=${this.location}`;
private path = 'jobs/feed';
private stackOverFlowJobsRssFeedUrl = `${this.hostName}/${this.path}?${this.query}`;
constructor(private http: HttpClient) { }
private getData$(url: string): Observable<any> {
const requestOptions = { headers: new HttpHeaders({ 'Access-Control-Allow-Headers': '*' }) };
return this.http.get<any>(url, requestOptions);
}
private get stackOverflowJobs$(): Observable<any> {
const proxyURL = `${this.proxyPrefix}/${this.path}?${this.query}`;
return fromFetch(proxyURL, {
selector: response => response.json()
});
}
ngOnInit(): void {
// http get
this.getData$(this.stackOverFlowJobsRssFeedUrl)
.subscribe((x) => {
console.log(x);
});
// rxJs fetch using proxy
this.stackOverflowJobs$.subscribe({
next: result => console.log('jobs:',result),
complete: () => console.log('done')
});
}
proxy.config
{
"/api/jobs": {
"target": {
"host": "https://stackoverflow.com",
"protocol": "https:",
"port": 443
},
"secure": false,
"changeOrigin": true,
"logLevel": "info"
}
}
Error
[HPM] Error occurred while trying to proxy request /api/jobs/feed?location=sydney from localhost:4200 to https://stackoverflow.com (ENOTFOUND) (https://nodejs.org/api/errors.html#errors_common_system_errors)