I was having a ton of issues with CORS in my react app and researched a lot and found out I had to use a proxy.
I tried using a proxy in package.json by adding
"proxy": "https://api.clashroyale.com/",
Then I tried deleting the above and creating setupProxy.js with the following content:
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
app.use(
'/v1/players',
createProxyMiddleware({
target: 'https://api.clashroyale.com/',
changeOrigin: true,
})
);
};
And my request looks like this in app.js and this happens when a button is clicked:
const clicksearch = () => {
const headers = {
"Authorization": "Bearer token",
"Content-Type": "application/json",
}
axios.get("v1/players/#123TAG", {headers})
.then(response => {
console.log(response.data);
})
.catch((error) => {
console.log('error ' + error);
});
}
I also tried using https://api.clashroyale.com/v1/players/#123TAG in the .get but also no luck. Any hints on how to fix this would be amazing
The response I get from the server when using https://api.clashroyale.com/v1/players/#123TAG as get request
Access to XMLHttpRequest at
'https://api.clashroyale.com/v1/players/%232Y900L99' from origin
'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
And the response I get when using v1/players/#123TAG is
GET http://localhost:3000/v1/players/%232Y900L99 403 (Forbidden)