Can't get body message after sending POST request to Netlify Function

Link to site: https://netlify-serverless-contact-form-test.netlify.app/

Hello,

On my local development setup I get my body and statusCode (400) for a bad request (invalid string from contact form).

However, when the site is deployed, I cannot get the body or the statusCode. Nothing gets sent back. Is this a CORS issue? Am I doing something wrong? Should I use other statusCodes?

What?

Edit: Actually, it’s receiving a http status code of 200 when submitting an empty contact form (from the front end). That’s not what I configured at all inside the srvless function. It’s supposed to be 400 :confused:

Edit 2: I ran the function without any query string parameters and got a 200 returned. That’s not at all what I put into my function:

HTTP Method Check:

if (event.httpMethod !== 'POST') {
    callback(null, {
      statusCode: 400,
      body: 'METHOD NOT ALLOWED',
      headers: {
        'Allow': 'POST'
      }
    })
  }

queryStringParameters validator:

if (!email || !name || !message) {
  callback(null, {
    statusCode: 400,
    body: 'Name, email, and message are required.'
  })
}

Hi @oyvindremme and sorry to be slow to get back to you!

Could you give us some more details so we can better investigate? We don’t generally advise on code (sometimes our customers do, though I can see nobody has stepped up yet), but we can make sure all the pieces are well connected. I don’t see you as using netlify form handling at all, but instead it appears your form is just posting to the function endpoint directly.

I do get a 200 response when I submit my filled-in form, but I also get one when I use a GET call which I understand from your code should not be happening. Perhaps you can use the pattern in this presumably working example (found from https://functions.netlify.com/examples/?search=POST), to derive a functional workflow?

Hi @oyvindremme. I’ve written a function that checks the HTTP method before and I was able to get things working: https://github.com/depadiernos/token-hider-inator/blob/master/src/lambda/getapi.js#L35.

That said, a good way to debug your function would be to console.log certain variables or values in your function and then viewing them in your function logs. For example: https://app.netlify.com/sites/netlify-serverless-contact-form-test/functions/contact-form-send-email. That should help you see what your function code is doing.

Hey, @fool. No problem, just glad you’re here!

The site is: https://netlify-serverless-contact-form-test.netlify.app/, btw

Endpoint in question: https://netlify-serverless-contact-form-test.netlify.app/.netlify/functions/contact-form-send-email

I may have been a little unclear. Let me try to steer this gracious host of a wreck to a somewhat clear direction.

The main issue is that once my site is deployed and built on Netlify, none of my Netlify Functions seem to run properly. I’m expecting different http status codes to return, but all I get are 200s. Take this function I refurbished and published on my Netlify testing site:

const querystring = require('querystring');

exports.handler = function(event, context) {
 
  if (event.httpMethod !== 'POST') {
    return {
      statusCode: 400,
      body: 'You are not using a http POST method for this endpoint.',
      headers: {
        'Allow': 'POST'
      }
    }
  }
  
  let body = event.queryStringParameters
  console.log(body.name)
  console.log(typeof body.name)

  if (body.name === "" || !body.name) {
    return {
      statusCode: 402,
      body: 'No name specified. Please specify a name.'
    }
  }

  return {
    statusCode: 200,
    body: `Hello, ${body.name}`
  }
}

I’m expecting a 400 if I use any other method than POST. I’m expecting a 402 if the name query string is missing or blank.

I hope that clarifies.

I’d like to note this is all working perfectly on my local development setup.

Hey @oyvindremme,
Is it possible that you’re missing a callback here? Could you try something like this:

exports.handler = function(event, context, callback) {
...
return callback(null, {
...

Someone deserves a raise!

That did it :slight_smile:

Is there a reason this is the way to do it?

1 Like

Sweet, glad to be helpful :raised_hands:

To your question about the callback: without the callback, you’re just working within the world of the function- you can log things, check things, etc. But the callback is a way, when all your logging and checking logic is complete, to do something with the results of that logic. Since you want to return a particular status code and body once you’ve done a check within the function context, you need to call the callback function in order to make that happen. There’s a bit more here:
https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html#nodejs-handler-sync

and this is a nice resource on callbacks in general:

I see! Glad I learned this.

But I see a lot of examples on the web only using return or just the callback() to return a response in the HTTP call. Did something change, or is this specific to how Lambda functions work?

I tend to price myself in knowing JavaScript pretty well, but this just flew right by me.

Thanks again :slight_smile:

I think you can do return callback or just callback but not just return- if you see examples of the latter, I’d be curious to see those! Very possible that something changed on our end, but nothing I’m aware of in the last few months.