Can't send POST request to lambda-functions when on live

Hello there !

So I’m building a function that I must send a “POST” request with some data in the body. It work all great in local but when I go on live no matter what I do the event.httpMethod is “GET”.

here is my .toml

[build]
functions = "functions"
[[redirects]]
  from= "/api/*"
  to= "/.netlify/functions/:splat"
  status= 200
[[headers]]
  for = "/*"
  [headers.values]
    Access-Control-Allow-Origin = "*"

and this is my test function

exports.handler = async function(event, context) {
  if (event.httpMethod !== "POST") {
    // To enable CORS
    console.log("preflight");
    return {
      statusCode: 200,
      body: "This was not a POST request!"
    };
  }
  console.log("hello");
  // console.log(event);

  console.log(event.headers["client-ip"]);
  console.log(event.body);

  return {
    statusCode: 200,
    body: JSON.stringify({ message: "hello there" })
  };
};

Hope someone can help. :wink:

Hi @MrSaky, welcome to the community!

If the request comes from a browser, the browser will first send an OPTION request. You’ll need to allow this method as it is required by CORS. You’ll also want to return that Access-Control-Allow-Origin header for this method. Once you do that, your POST should succeed.

Let me know if you have any other questions.

Hi @Dennis. So there was two problems :

  1. There was a conflict with Nuxt.js’s rooter and Netlify’s redirects
  2. I forget an avait before the fetch()

( to configue Nuxt.js’s rooter : File System Routing - NuxtJS )

PS: It wasn’t a CORS issue here but it would have been possible so if you through the same issue just do as Dennis said