Plugin api : error when using fetch with post, EVEN from the UI

I’m trying to make a http query to a back-end,
i solved all the CORS issues, and it works fine when i do a GET query.
However, when i try POST, i get the following message :
'<!DOCTYPE html>\n<html lang="en">\n<head>\n<meta charset="utf-8">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot POST /</pre>\n</body>\n</html>\n'

Here is my code


const response = fetch('http://localhost:1337', {
    method: 'POST',
    headers: {	
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(inputData)
 })
.then(result => {
		console.log(result.text());
		console.log(result);
});

Things i have tried :

I’ve seen a bunch of unresolved issue posted around that topic,
It seems quite unclear for a lot of people, it would be nice if the documentation could go more in depth (not just showing a GET example for example), i think a lot of people might be struggling with that.

Thanks in advance for the help.

1 Like

I was able to get it working by removing the
‘Accept’: ‘application/json’,
from the headers

how did you manage to solve the cors issues ?
now i get a cors issue when trying to POST (no matter what headers combination i try)

on the node/express serve side, i already added :

res.setHeader('Access-Control-Allow-Origin', '*');
		res.setHeader('Access-Control-Allow-Headers', '*');
		res.setHeader('Access-Control-Allow-Credentials', true);
		res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS, PUT, DELETE');
		
		next();

This is how my server was set up

router.use((req, res, next) => {
   res.setHeader('Access-Control-Allow-Origin', '*');
   res.setHeader('Access-Control-Allow-Headers', 'origin,X-Requested-With,Content-Type,Accept,Authorization');
  if(req.method === 'OPTIONS'){
  res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS, PUT, DELETE');
}
next()
})

The api request :
const response = fetch(‘URL’, {
headers: {
‘Content-Type’: ‘application/json’
},
method: ‘POST’,
body: JSON.stringify(inputData)})

respone.then((res) => {
res.json().than((data)=>{ console.log(‘res data’, data)}})
}).catch(err) => { console.log(‘error:’) err}

const response = fetch(‘URL’, {
headers: {
‘Content-Type’: ‘application/json’
},
method: ‘POST’,
body: JSON.stringify(inputData)})

respone.then((res) => {
res.json().than((data)=>{ console.log(‘res data’, data)}})
}).catch(err) => { console.log(‘error:’) err}

i just found out i was doing

app.get('/', async (req, res) => {

instead of post,
looks like the error was between the chair and the computer,
thanks for encouraging me to keep trying !