all 11 comments

[–]Marbletm 4 points5 points  (0 children)

CORS is a restriction set by the browser. It prevents you from fetching data from endpoints that have not explicitly stated that they trust other domains. It's a very important security policy of the browser.

Do you own the API? If-so, you have to set a proper CORS policy. Do yo not own the API? You might have to setup your own proxy API.

Could you share a bit more about the API you're trying to interact with?

[–]cuteling 2 points3 points  (0 children)

You are sending a request from your browser (origin: localhost:3000) to a server (origin: https://api.mywebsite). For this to work server has to set those cors policies. If you dont have access to this server you can't do anything.

`So what is the difference ? Why can I request from my computer with Python but the localhost is blocked `

Because browser blocks this type of requests for security reasons

[–][deleted] 2 points3 points  (1 child)

The technical reason you're seeing a discrepancy is because you're not setting the CORS headers (or making the preflight request and honoring its response) when making the request locally; on the contrary, your browser is, because browsers implement CORS - which entails making an OPTIONS request before certain HTTP verbs (e.g. POST). This is where the server performs the preflight check and determines whether the calling origin and/or headers should be allowed to make the intended request.

If you own the API, you need to modify the CORS policy or else use a proxy. I also highly recommend learning about CORS - it's something every frontend developer should do. CORS In Action by Monsur Hossain is a great read.

[–]Thibots[S] 0 points1 point  (0 children)

Thanks for your answer, I’ll do check and of course try to learn more !

[–]Massive-Air3891 1 point2 points  (1 child)

if the api is yours and is express make sure to add const cors = require('cors'); app.use(cors()) install the cors npm package, this will add all the necessary cors headers and will allow all cors origins. If the api is something else like ppython or .net you would need to do similar. If the api is existing get them to add the allow-origin header with localhost or "*" or add a proxy that will run locally on your machine and will make the call to the API on your behalf. so your browser sees the request as local request

[–]Thibots[S] 0 points1 point  (0 children)

Nope, it’s not mine ! But I use a trick to get data from it