Home > Software engineering >  POST request is working fine with postman but not in website
POST request is working fine with postman but not in website

Time:02-04

export const executeCode = (data) => {
       return fetch("https://ide.geeksforgeeks.org/main.php",{
              method: "POST",
              headers: {
                     Accept : "application/json",
                     "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
              },
              body: data
       })
       .then(response => response.json())
       .catch(err => console.log(err))
}

I'm sending a POST request to G4G's compiler API. It's working fine in postman but not working in my react app.

Error msg (image)

working in postman (image)

CodePudding user response:

Add this line of code on backend in .htacess file

<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "POST,GET,OPTIONS,DELETE,PUT"
Header set Access-Control-Allow-Headers "*"
</IfModule>

CodePudding user response:

When making a call from an website there are security considerations to be taken into account. One of them is CORS.

In a nut shell the browser asks the server if it allows HTTP calls (calls that are considered not simple actually).

Postman works because it doesn't do this check.

G4G need to respond to OPTIONS requests with a proper CORS response that allows your host to call them.

CodePudding user response:

Please add to the header of the request Access-Control-Allow-Origin: * let me know if it's working

  •  Tags:  
  • Related