Back

Body and Query are Empty when calling trough API, is OK when called by URL or by executed directly

  • 0
  • Functions
  • REST API
  • Cloud
jp123cl
30 Mar, 2024, 21:29

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:

TypeScript
 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:

TypeScript
{
  "data": "{\"name\":\"JP\",\"email\":\"jtraverso@gmail.com\",\"content\":\"hola\"}"
}

Response:

TypeScript
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
TL;DR
Developers are facing issues when calling the API POST Endpoint as the body and query parameters are empty. However, when executed manually, it works fine. The issue seems to be related to the headers and content type not being recognized properly when calling through the API. The solution might involve checking and ensuring the correct content type is set when making the API call.
jp123cl
30 Mar, 2024, 21:32

``` "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"````

jp123cl
30 Mar, 2024, 21:33
TypeScript
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);
}```
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more