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
- Sign In With Apple OAuth Help
Hi All! I've got a flutter & appwrite app which Im trying to use sign in with apple for. I already have sign in with google working and the function is the sam...
- Custom Domains
Hi All, Should be a quick config issue. I'm setting up custom domains on the hosted version. I have verified the domain with the CNAME but appwrite isn't gene...
- custom domain with CloudFlare
Hi all, it seems that CloudFlare has blocked cross-domain CNAME link which made my app hostname which is in CloudFlare, unable to create a CNAME pointing to clo...