Troubleshooting CORS Errors on DataHub
Find ready to use proxy to troubleshoot the CORS errors on DataHub
CORS Errors are common when you’re working with APIs but it’s very important to handle them effectively due to several security reasons.
What does it look like?
1
Access to fetch at 'https://solana--mainnet--rpc.datahub.figment.io/' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Copied!
What it is? - Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any other origin (domain, scheme, or port) than its own from which a browser should permit loading of resources. CORS also relies on a mechanism by which browsers make a β€œpreflight” request to the server hosting the cross-origin resource, in order to check that the server will permit the actual request. In that preflight, the browser sends headers that indicate the HTTP method and headers that will be used in the actual request.
An example of a cross-origin request: the front-end JavaScript code served from https://domain-a.com uses XMLHttpRequest to make a request for https://domain-b.com/data.json .
For security reasons, browsers restrict cross-origin HTTP requests initiated from scripts. For example, XMLHttpRequest and the Fetch API follow the same-origin policy. This means that a web application using those APIs can only request resources from the same origin the application was loaded from unless the response from other origins includes the right CORS headers.
Could not load image
The CORS mechanism supports secure cross-origin requests and data transfers between browsers and servers. Modern browsers use CORS in APIs such as XMLHttpRequest or Fetch to mitigate the risks of cross-origin HTTP requests. Source - Mozilla MDN Web Docs​
How to handle them when working with Figment’s DataHub?
There are two major ways to handle CORS errors effectively so you don’t expose your API keys and credentials on the Client-Side.
  1. 1.
    Use a proxy - One solution for making cross-origin requests is to use a CORS proxy to make it seem as though you’re making the request from a location that’s allowed.
There are multiple CORS proxies (Must Check) out there that you can use for free. Some of them are
  1. 1.
    Use a serverless function - Using a serverless function is another more effective way to handle the CORS errors and proxy our requests and here in this we build our own functions or micro-infrastructure to call a web service and interact using APIs. Azure, AWS & GCP are most popular for running serverless functions. Sharing some examples of serverless functions built and shared by our community members.
i. Azure Function for CORS proxy by Florian Uhde - Check the Gist HERE​
1
const axios = require('axios');
2
const decode = require('unescape');
3
​
4
module.exports = async function (context, req) {
5
var route = decode(context.bindingData.route || req.params.restOfPath);
6
let params = req.query
7
delete params['code']
8
const request = {
9
url: 'https://secret-2--lcd--full.datahub.figment.io/apikey/xxxxxxxxxxxxxxxx/' + route,
10
method: req.method,
11
data: req.rawBody,
12
params: params
13
}
14
const response = await axios(request)
15
context.res = {
16
body: response.data
17
};
18
}
Copied!
ii. AWS Lambda Function for CORS proxy by Austin Woetze- Check the Gist HERE​
1
const axios = require('axios');
2
​
3
exports.handler = async (event) => {
4
​
5
const route = event.path
6
const request = {
7
url: 'https://secret-2--lcd--full.datahub.figment.io/apikey/xxxxxxxxxxxxxxxxxxxxxxxx' + route,
8
method: event.httpMethod,
9
data:{}
10
}
11
const response = await axios(request)
12
​
13
var res = {
14
"statusCode": 200,
15
"headers": {
16
"Access-Control-Allow-Origin":"*"
17
},
18
"body": (JSON.stringify(response.data)),
19
"isBase64Encoded": false
20
};
21
return res;
22
​
23
};
Copied!
iii. Template if you want to host your own nginx server - DataHub CORS Development Proxy​
If you have any other solution for the same feel free to share it with us on our discord server. Thanks in advance, we appreciate your contribution.
References -
Copy link