CORS from anywhere

CORS from anywhere

Have you ever struggled with CORS error messing up your website and just wanted to get it working? Thankfully, there is a service for that called CORS Anywhere which is a simple API that enables cross-origin requests to anywhere. It works by proxying requests to these sites via a server.

Thus, all you have to do to work around CORS is to prepend the URL you want to access with https://cors-anywhere.herokuapp.com/ and spoof an origin header. For example, instead of writing axios.get('https://example.com') you would write as below:

axios({
    method: 'get',
    url: 'https://cors-anywhere.herokuapp.com/https://example.com/',
    headers: {'Origin': 'https://example.com'}
})

This makes a call to https://example.com with origin header set to the one that satisfies CORS policy requirements, and https://cors-anywhere.herokuapp.com returns us the result. Simple yet elegant solution.

If you don't want to rely on a 3rd party, you can also set up CORS Anywhere on your machine using npm module cors-anywhere. Of course, at this stage you may just as well set up your own proxy on your backend but if for whatever reason you don't want to do that, keep this option in mind.