curl --location 'https://fra.cloud.appwrite.io/v1/functions/custom-login/executions'
\
--header 'X-Appwrite-Project: '
\
--header 'Content-Type: application/json'
\
--header 'X-Appwrite-Key: '
\
--data-raw '{
"email": "",
"pin": 123
}'
Appwrite functions are unable to read json body.
Nodejs : 22 used starter sample
I got stuck since 4 hours to get started with Simple function which accepts body from the client apps.
Can anyone help me on this ?
import * as sdk from "node-appwrite"; import bcrypt from "bcryptjs";
export default async ({ req, res, log, error }) => { const client = new sdk.Client() .setEndpoint(process.env.APPWRITE_ENDPOINT) .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID) // .setKey(req.headers['x-appwrite-key'] ?? ''); .setKey(process.env.APPWRITE_API_KEY); // API key must be from a privileged scope
const db = new sdk.Databases(client);
const users = new sdk.Users(client);
try {
const response = await users.list();
log(Total users: ${response.total});
} catch(err) { error("Could not list users: " + err.message); }
let body = {};
try {
log(req: ${req});
log(req.bodyRaw: ${req.bodyRaw});
log(req.bodyText: ${req.bodyText});
log(req.bodyJson: ${req.bodyJson});
log(req.bodyText);
log(JSON.stringify(req.bodyJson));
log(JSON.stringify(req.headers));
log(req.scheme);
log(req.method);
log(req.url);
log(req.host);
log(req.port);
log(req.path);
log(req.queryString);
log(JSON.stringify(req.query));
body = JSON.parse(req.body || '{}'); } catch (e) { log(e.message) return res.json({ error: 'Invalid JSON' }); }
**Response: ** Total users: 17 -> req: [object Object] req.bodyRaw: [Blank] req.bodyText: [Blank]
Recommended threads
- Realtime api and labels as permission
in my tables i set labels as permission and real-time capabilities stopped working. Before when i was having "any" role everything was working. Note: user have...
- Sveltekit + Bun sites runtime not deploy...
anyone tried deploying sveltekit + bun sites? its building but not running, and the only thing from build console to value is ``` > Using svelte-adapter-bun .s...
- Domain is owned by a different organizat...
I was trying to add a domain to one of my projects however, it keeps giving me the error of Domain is owned by a different organization. I did have a self hoste...