Hi, I am trying to make a post
request using fetch to one of my functions but the console tells me it is unsupported with the following message:
POST http://localhost:7000/.netlify/functions/my_func 501 (Unsupported method ('POST'))
I am making an API call posting a json payload, and need to wait for it’s response in the function, thus did not want to use forms.
fetch("/.netlify/functions/my_func", {
method: 'POST',
headers: {
Accept: "application/json",
},
body: JSON.stringify(my_json)
})
.then(data => return data.json() });
…
exports.handler = async(event, context, callback) => {
if (event.httpMethod !== "POST") {
console.log(event.httpMethod);
return { statusCode: 405, body: "Method Not Allowed" };
}
return {
statusCode: 200,
body: "Ok"
}
}