I have created an Function in my Appwrite project. The function is not receiving the payload I attempt to give it through an HTTP-Post request (Postman). I have created a function which takes ONE property as an example to illustrate the problem.
The output of the function is as follows when called:
As you can see the payload is empty. The call looks like this:
As you can see I am giving it a payload. The code of the function is this:
module.exports = async function (req, res) {
const client = new sdk.Client();
try{
client
.setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
.setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
.setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
.setSelfSigned(true);
// Detect if Payload is present
if(!req.payload){
res.json({"error": "no payload", "payload": req.payload}, 400)
}
// Read in payload
const payload = JSON.parse(req.payload)
// Check if all requested payload methods are present
if(!payload.maxNumber) {
res.json({success: false, message: "Invalid payload."}, 400);
return;
}
// Create random number (Yes I know this won't work because the Payload is a string, but this is just an example and I'm trying to at least get the payload to be detected :-)
const randomNumber = Math.floor(Math.random() * payload.maxNumber);
// Send result
res.send({success:true, number: randomNumber})
}catch(e){
res.json({success: false, message: "Unexpected error: " + e, payload: req.payload}, 400);
}
};
I've been attempting to solve this issue for a few hours now. I hope someone is able to assist in this, I'd be very grateful!
It doesn't look like you're passing the payload to the API correctly. According to the docs, the payload should be passed in via the data
parameter and it should be a string. Here's an example HTTP request from the docs:
POST /v1/functions/{functionId}/executions HTTP/1.1
Host: HOSTNAME
Content-Type: application/json
X-Appwrite-Response-Format: 1.0.0
X-Appwrite-Project: 5df5acd0d48c2
X-Appwrite-JWT: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ...
{
"data": "[DATA]",
"async": false
}