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
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 Functions examples - Netlify Functions), to derive a functional workflow?
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: Netlify App. That should help you see what your function code is doing.
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.
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:
and this is a nice resource on callbacks in general:
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.
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.