Hello! I’m getting a strange CORS error when I’m trying to invoke one of the cloud functions I created with Firebase. I have the following cloud function:
export const getUser = functions.https.onRequest(async (req, res) => {
res.set("Access-Control-Allow-Origin", "http://127.0.0.1:5501");
res.set("Access-Control-Allow-Headers", "Content-Type");
console.log("getUser() function is invoked");
const method = req.method;
const body = req.body;
const address = body.address;
res.json({method: method, body: address});
return;
});
(The function looks a little bit strange now perhaps but It’s just for testing invocation and reading the request message).
I’m invoking the function with the following code:
function runGetUser() {
axios({
method: 'POST',
url: 'my_url',
data: { "address": '0x8038494b070d0Fedf948bdF15a2580a99EFa7b47' },
}).then((response) => {
console.log(response.data);
}).catch((error) => {
console.log(error);
});
};
And i’m getting the following response nicely:
{method: ‘POST’, body: ‘0x8038494b070d0Fedf948bdF15a2580a99EFa7b47’}
However, when I’m trying to use ethers library to make sure the address is converted to checksum before I’m doing anything else using the following code:
export const getUser = functions.https.onRequest(async (req, res) => {
res.set("Access-Control-Allow-Origin", "http://127.0.0.1:5501");
res.set("Access-Control-Allow-Headers", "Content-Type");
console.log("getUser() function is invoked");
const utils = ethers.utils;
const method = req.method;
const body = req.body;
const address = body.address as any;
const userAddress = utils.getAddress(address);
res.json({method: method, body: userAdress});
return;
});
, it’s throwing me the following error:
Access to XMLHttpRequest at ‘http://127.0.0.1:5001/cloud-functions-typescri-772a4/us-central1/user-getUser’ from origin ‘http://127.0.0.1:5501’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: It does not have HTTP ok status.
index.js:9 F {message: ‘Network Error’, name: ‘AxiosError’, code: ‘ERR_NETWORK’, config: {…}, request: XMLHttpRequest, …}
axios.min.js:1 POST http://127.0.0.1:5001/cloud-functions-typescri-772a4/us-central1/user-getUser net::ERR_FAILED
And I cannot understand why I would even get a CORS-error when I have only changed some logic inside the invoked function.