I've been developing a personal fortnite stats web app. I you use two main API calls. One of them has CORS access policy enabled, so in develoment I can get around this by adding the domain as a proxy and adding the path within the fetch request.
When I build this I cannot call the API, so have tried sending the full URL in the fetch request plus adding headers, mode etc to try and meet the CORS policy. I have tried everything but its still not working.
Could anybody help please?
Thanks
app.js level
fetchFortniteData = username => {
return new Promise((resolve, reject) => {
fetch(`/v1/profile/pc/${username}`, {
headers: new Headers({
'TRN-Api-Key': process.env.REACT_APP_TRN
})
})
package.json
"proxy": "https://api.fortnitetracker.com",
The errors I receive from the console are:
Access to fetch at 'https://api.fortnitetracker.com//v1/profile/pc/popps01' 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.
2fortniteApi.js:57 OPTIONS https://api.fortnitetracker.com//v1/profile/pc/popps01 404
Which is what was happening in development, which is why I added a proxy.
I have also added mode: 'no-cors' and other allow origin keys to the header but it still doesnt work.
In the network tab:
Request URL: https://api.fortnitetracker.com//v1/profile/pc/popps01 Request Method: OPTIONS Status Code: 404 Remote Address: [2606:4700:20::6819:9810]:443 Referrer Policy: no-referrer-when-downgrade Provisional headers are shown Access-Control-Request-Headers: trn-api-key Access-Control-Request-Method: GET Origin: http://localhost:3000 Referer: http://localhost:3000/ User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1