Netlify site: https://whatsapp-gp.netlify.app/
Hello i have an axios http request inside the app.post handler which sends a whatsapp message.
When i send a post request via postman, the whatsapp message is sent, but postman returns a 502 error saying “Error decoding lambda response: invalid status code returned from lambda: 0”.
I have tried to apply the suggestion made in this post , with no results.
This is my code:
app.post("/.netlify/functions/functionname", async function (req, res) {
var data = JSON.stringify({
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": "number",
"type": "text",
"text": {
"body": "ciao"
}
});
var config = {
method: 'post',
url: 'https://graph.facebook.com/v13.0/id/messages',
headers: {
'Authorization': 'Bearer token',
'Content-Type': 'application/json'
},
data : data
};
const pass = (body) => {callback( null, {
statusCode: 200,
body: JSON.stringify(body)
})};
const mess = await axios(config)
.then((response) => {
console.log(response.data)
pass(response.data)
})
.catch((error) => ({
statusCode: 500,
body: JSON.stringify(error),
}));
})
Could anyone point me in the right direction?
Thanks
Hey @andsic
A Netlify function must return a response, e.g.
exports.handler = async (event, context) => {
return {
statusCode: 200,
body: "Hello, World"
};
};
(See Functions Playground for more.)
It appears you are returning a status in pass
, and mess
, however neither of these are returned by the function.
andsic
June 9, 2022, 10:35am
3
Hello, thank you very much for your reply.
I also tried returning everything as showed in this post .
Specifically i tried to do this:
return axios(config)
.then((response) => {
console.log(response.data);
return {
statusCode: 200,
body: JSON.stringify(response.data),
};
})
.catch((error) => {
console.log(error);
return {
statusCode: 500,
body: JSON.stringify(error.message),
};
});
But the result is always the same. Probably due to my poor coding skills! But i can’t really figure it out.
The thing missing from this code that is present in the linked thread is
export.handler = async (event, context) => {
// insert code here.
}
The handler
is required .
andsic
June 9, 2022, 11:19am
5
Hello i tried inserting in exports.handler but stll the same.
Should i use exports.handler if i’m running express.js and serverless-http?
This is what i tried:
app.post("/.netlify/functions/functionname", async function (req, res) {
var data = JSON.stringify({
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": "number",
"type": "text",
"text": {
"body": "ciao"
}
});
var config = {
method: 'post',
url: 'https://graph.facebook.com/v13.0/id/messages',
headers: {
'Authorization': 'Bearer token',
'Content-Type': 'application/json'
},
data : data
};
exports.handler = async (event, context) => {
return axios(config)
.then((response) => {
console.log(response.data);
return {
statusCode: 200,
body: JSON.stringify(response.data),
};
})
.catch((error) => {
console.log(error);
return {
statusCode: 500,
body: JSON.stringify(error.message),
};
}); };
})```
The exports.handler
needs to wrap the entire function as is demonstrated in the functions playground link previously posted and the thread you have linked to
E.G.
const axios = require('axios');
exports.handler = async (event, context) => {
const url = event.queryStringParameters.url;
console.log("URL: ", url)
return axios({
method: "get",
url: url,
})
.then((response) => {
console.log("data", response.data);
return {
statusCode: 200,
body: JSON.stringify(response.data),
};
})
.catch((error) => {
console.log(error);
return {
statusCode: 500,
body: JSON.stringify(error.message),
};
});
}
Can you share the repository you are deploying?
andsic
June 9, 2022, 11:34am
7
Found it! It was a very silly mistake from me, i forgot to end the app.post with res.send()
very sorry about it. Btw i never did this exports.handler thing, even in my other functions.
Maybe because i have:
const serverless = require('serverless-http');
const app = express();
and
module.exports = app;
module.exports.handler = serverless(app);
But not sure?
Express works the following way:
import express from 'express'
import {Router} from 'express'
import serverless from 'serverless-http'
export async function handler(event, context) {
const app = express()
const router = Router()
api.use('/api/', router)
return serverless(app)(event, context).then(result => {
return result
})
}
You need to add the following to your netlify.toml
:
[functions]
node_bundler = "esbuild"
andsic
June 9, 2022, 12:37pm
9
Hey there, my lambdas, even other ones i set up, seem to work fine without the export handler.
In all of them i have:
require('dotenv').config()
const express = require("express");
const axios = require('axios').default;
const serverless = require('serverless-http');
const app = express();
app.use(express.urlencoded({ extended: false }));
Then the app.post, app.get ecc.
And it ends with:
app.use((error, req, res, next) => {
res.status(500)
res.send({error: error})
console.error(error.stack)
next(error)
})
module.exports = app;
module.exports.handler = serverless(app);
Nothing else, but it works fine with no errors.
rpeltz
June 9, 2022, 10:51pm
10
I use async/await in my serverless functions since they usually are async. You can use it with a call to axios. Something like this; const result = await axios({method: "get",url: url)
andsic
June 10, 2022, 8:05am
11
Yes, that is what i usually do and what i did, i used return just because that lambda error took me down the wrong path, not realizing i didn’t end the post handler with res.send