16

I am trying to send a get request to the Wikipedia API. I am sending the request form a angular frontend so i'm trying to use the Heroku CORS Anywhere endpoint to avoid CORS issues. For some reason, I'm still getting a 503 response saying no access-control-allow-origin header is present on the requested resource. Any idea why this would happen/what else I can try?

My code:

import { Injectable } from '@angular/core';
import { Http, Response, } from '@angular/http';
import { Observable } from 'rxjs/Rx';



@Injectable()
export class RestService {
    API_URL: string = 'https://cors-anywhere.herokuapp.com/https://en.wikipedia.org/wiki/';

  constructor(private http: Http) { }

  public getRandomArticle() : Observable<any> {
        return this.http.get(`${this.API_URL}Special:Random`)
        .map((res: Response) => res.json())
        .catch((err: any) => Observable.throw(err || 'server error'));
  }

}
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
user2094257
  • 1,645
  • 4
  • 26
  • 53
  • 2
    About Wikipedia in particular, you know that just by adding `origin=*` to the query parameters for a Wikipedia API URL, you can actually make cross-origin requests to Wikipedia work? See https://stackoverflow.com/questions/37106041/does-wikipedia-api-support-cors-or-only-jsonp-available/37109743#37109743 – sideshowbarker Nov 02 '17 at 23:26

3 Answers3

73

You can deploy a CORS Anywhere server to Heroku in just 2-3 minutes, with 5 commands:

git clone https://github.com/Rob--W/cors-anywhere.git
cd cors-anywhere/
npm install
heroku create
git push heroku master

After running those commands, you’ll end up with your own CORS Anywhere proxy running at, e.g. https://cryptic-headland-94862.herokuapp.com/. So then instead of prefixing your request URL with https://cors-anywhere.herokuapp.com, prefix it with your proxy’s URL.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
  • 4
    Thanks a lot for your answer. I think this is the most perfect/comprehensive answer i've ever received on here... – user2094257 Nov 03 '17 at 08:43
  • 1
    How do you use this proxy to whitelist your own domain? – ReinstateMonica3167040 Feb 17 '19 at 01:47
  • 1
    And for those without `heroku` yet installed run this in your terminal (on a Mac, untested on other machines): `brew tap heroku/brew && brew install heroku` - it may take a few minutes to complete – maxshuty Dec 21 '20 at 17:04
  • Additionally I had to `npm install heroku` before `heroku create` which after installation opened a browser where I had to create an account. – ABGR Oct 18 '21 at 17:33
  • 2
    Heroku free tier is not available anymore.. there's any other free option to set this 'cors-anywhere' server? – JS_LnMstr Dec 06 '22 at 15:26
  • This option costs money after a certain amount of requests. – cs95 Apr 09 '23 at 06:14
1

In response to this I wanted to give a more detailed response for Windows users:

Windows Requried Items

After everything above is done, issue the following commands

Open a new terminal, then:

heroku login
git clone https://github.com/Rob--W/cors-anywhere.git
cd cors-anywhere/
npm install
heroku create
git push heroku master

It will process and update/upload and you will get a app URL:

https://some-name-giveng.herokuapp.com/
Mike Q
  • 6,716
  • 5
  • 55
  • 62
0

This is because the public demo server (cors-anywhere.herokuapp.com) is limited by January 2021 https://github.com/Rob--W/cors-anywhere/issues/301

Here is my own proxy server

https://fast-dawn-89938.herokuapp.com/

You can use like: https://fast-dawn-89938.herokuapp.com/https://your-domain.com

demo: https://fast-dawn-89938.herokuapp.com/https://google.com

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Abdul Rehman
  • 139
  • 1
  • 5