Body and Query are Empty when calling trough API, is OK when called by URL or by executed directly
- 0
- Functions
- REST API
- Cloud

Im just trying to print query Parameters or Json data parameters as output, but body and query are always empty when called using API POST Endpoint.
Executing Manually works:
req:[object Object]
payloadundefined
bodyRaw:{
"data": "{\"name\":\"testName\",\"email\":\"test@email.com\",\"content\":\"testContent\"}"
}
body:"{\n \"data\": \"{\\\"name\\\":\\\"testName\\\",\\\"email\\\":\\\"jtest@email.com\\\",\\\"content\\\":\\\"testContent\\\"}\"\n}"
headers:{"host":"66087cd8e79f8:3000","x-appwrite-trigger":"http","x-appwrite-user-id":"66081b69033c6a######","x-appwrite-user-jwt":"##########JKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiI2NjA4MWI2OTAzM2M2YTM2YWFhYSIsInNlc3Npb25JZCI6IjY2MDgxYjg5Mjg4NGRlODkxYzQxIiwiZXhwIjoxNzEx##########.40qodo##########-NCeOGOA2YwvSoLhouBmQC#####","x-appwrite-continent-eu":"false","content-type":"application/x-www-form-urlencoded","connection":"keep-alive","content-length":"88"}
scheme:http
method:POST
url:http://66087cd8e79f8:3000/
host:66087cd8e79f8
port:3000
path:/
queryString:
query:{}
Content-Type: application/x-www-form-urlencoded
Parsed form data: {"{\n \"data\": \"{\\\"name\\\":\\\"testName\\\",\\\"email\\\":\\\"test@email.com\\\",\\\"content\\\":\\\"testContent\\\"}\"\n}":""}
response: {"{\n \"data\": \"{\\\"name\\\":\\\"testName\\\",\\\"email\\\":\\\"test@email.com\\\",\\\"content\\\":\\\"testContent\\\"}\"\n}":""}
Same called by API (sending X-Appwrite-Project=MYPROJECT, X-Appwrite-Key=MYKEY, Content-Type=application/json, and raw data as:
{
"data": "{\"name\":\"JP\",\"email\":\"jtraverso@gmail.com\",\"content\":\"hola\"}"
}
Response:
req:[object Object]
payloadundefined
bodyRaw:
body:""
headers:{"host":"cloud.appwrite.io","x-appwrite-trigger":"http","x-appwrite-continent-eu":"false","connection":"keep-alive","content-length":"0"}
scheme:http
method:POST
url:http://cloud.appwrite.io/
host:cloud.appwrite.io
port:80
path:/
queryString:
query:{}
Content-Type: undefined

``` "logs": " req:[object Object]\n payloadundefined\n bodyRaw:\n body:\n headers:{host:cloud.appwrite.io,x-appwrite-trigger:http,x-appwrite-continent-eu:false,connection:keep-alive,content-length:0}\n scheme:http\n method:POST\n url:http://cloud.appwrite.io/\n host:cloud.appwrite.io\n port:80\n path:/\n queryString:\n query:{}\nContent-Type: undefined"````

const html = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Contact Form</title>
</head>
<body>
<form action="/" method="POST">
<input type="text" id="name" name="name" placeholder="Name" required>
<input type="email" id="email" name="email" placeholder="Email" required>
<textarea id="content" name="content" placeholder="Message" required></textarea>
<button type="submit">Submit</button>
</form>
</body>
</html>`;
export default async function ({ req, res, log, error }) {
log(" req:" +req);
log(" bodyRaw:" + req.bodyRaw);
log(" body:" + JSON.stringify(req.body));
log(" headers:" + JSON.stringify(req.headers));
log(" method:" + req.method);
log(" url:" + req.url);
log(" path:" + req.path);
log(" queryString:" + req.queryString);
log(" query:" + JSON.stringify(req.query));
const contentType = req.headers['content-type'];
log("Content-Type: " + contentType);
if (req.method === 'GET') {
return res.send(html, 200, {'content-type': 'text/html'});
} else if (req.method === 'POST') {
let formData = {};
if (req.headers['content-type'] === 'application/x-www-form-urlencoded') {
formData = querystring.parse(req.bodyRaw);
} else if (req.headers['content-type'] === 'application/json') {
const body = JSON.parse(req.bodyRaw);
if (body.hasOwnProperty('data')) {
formData = JSON.parse(body.data);
} else {
formData = body;
}
} else {
return res.send('Unsupported Content-Type', 400);
}
log("Parsed form data: " + JSON.stringify(formData));
log("Sresponse: " + JSON.stringify(formData));
return res.json(formData, 200, {'content-type': 'application/json'});
}
return res.send('Not found', 404);
}```
Recommended threads
- User Queries not working
When I try to use queries on users, it gives error saying invalid query method. Now, I dont know whether it is possible or not to query users or it’s just some...
- appwrite cli alpine os
the appwrite cli does not work on alpine os if you install it using the recommended bash script. Maybe there is the possibility to compile it for alpine using t...
- Email OTP Mail Getting Delayed by 10 min...
I just noticed I am reciving delayed otp emails on frankfurt server we are on free plan now but we are planning to change to get on to paid plan can anyone plea...
