-1

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)
Prasanna
  • 1
  • 1
  • 3

2 Answers2

0

Are you sure that CORS is allowed on the SO server-side? Try to send this from SO and another domain console in dev tools, you will get a response only from SO. One of the options is to set up some server and use it as middleware, then the CORS issue will be not relevant

fetch('https://stackoverflow.com/jobs/feed')
  .then(function(response) {
    return response.text();
  }).then(function(data) {
    console.log(data); // this will be a string
});
Liveindream
  • 159
  • 1
  • 4
  • I guess CORS is not allowed at the server end, that why I'm trying with proxy. It should be possible with proxy, but don't know how to achieve it successfully – Prasanna Feb 19 '21 at 11:05
-1
{
  "/proxy/*": {
  "target": "https://stackoverflow.com",
  "secure": false,
  "changeOrigin": true,
  "logLevel": "debug",
  "pathRewrite": {"^/proxy" : ""}
  }
}

This proxy config solved my problem :)

StackOverflow Ref: Angular-CLI proxy to backend doesn't work

Prasanna
  • 1
  • 1
  • 3