Back

[SOLVED] Can't get body data in the req module for node.js

  • 0
  • Functions
mc-stephen
20 Sep, 2023, 17:33

This is the code to print all value

TL;DR
The user had an issue where they couldn't get body data in the req module for node.js. They provided code and the output they received. It turns out that req.body will only be parsed if the header specifies that the content type is application/json. The user was initially confused about this behavior but eventually found a solution by parsing req.bodyRaw as JSON. The issue was resolved.
mc-stephen
20 Sep, 2023, 17:34

And the first img is the output it gave to me

Drake
20 Sep, 2023, 17:45

req.bodyRaw is a string so when you stringify that, jsonBody results in a nested string. When you parse that, parsedJsonbody becaomes a string

Drake
20 Sep, 2023, 17:46

please try using req.body.hi

mc-stephen
20 Sep, 2023, 17:46

Okay, let me try it and get back to you

RedLeeder
20 Sep, 2023, 17:52

As an aside, I could not get

TypeScript
req.body.whatever

to work. I had to do

TypeScript
let parsedBodyRaw = JSON.parse(req.bodyRaw);
// And then I could access 'whatever' via
parsedBodyRaw.whatever
mc-stephen
20 Sep, 2023, 18:07

Okay will try that out

mc-stephen
20 Sep, 2023, 18:07

Still didn't work, let me try the other guy suggestion, and see how it goes

mc-stephen
20 Sep, 2023, 18:16

πŸ’―πŸ’―πŸ’―πŸ’―πŸ’―β€οΈβ€οΈβ€οΈβ€οΈβ€οΈβ€οΈ Thanks so much, It works I have been debugging this for a very long time, and you kinda make it look easy.

I appreciate your time ❀️

mc-stephen
20 Sep, 2023, 18:17

Hi, so I have gotten it to work, thanks for joining in to debug this with me I really appreciate this ❀️❀️❀️❀️❀️❀️❀️❀️,

πŸ’― To appwrite once again

mc-stephen
20 Sep, 2023, 18:19

Can't get body data in the req module for node.js

RedLeeder
20 Sep, 2023, 18:21

Yep, I probably spent a good 30 minutes banging my head against the wall before figuring that out. Not entirely sure why the req.body route doesn't work

mc-stephen
20 Sep, 2023, 18:32

Yer, me too, made me question my field of study πŸ˜…. Not really sure if I would have guess this if you haven't said so.

mc-stephen
20 Sep, 2023, 18:32

Maybe the appwrite team need to look more into this.

RedLeeder
20 Sep, 2023, 18:44

Yeah, I would be curious to see if it's a "we're the dumb ones" or if there's actually a bug in the req module

Drake
20 Sep, 2023, 20:02

Not entirely sure why the req.body route doesn't work

what do you mean?

RedLeeder
20 Sep, 2023, 21:05

At risk of you highlighting something dumb that I've overlooked (as has been my habit the past couple days)..

TypeScript
export default async ({ req, res, log, error }) => {
  // these would just return nothing/empty string
  log(req.body.blah)
  log(JSON.stringify(req.body.blah))
  
  // these worked
  const parsedBodyRaw = JSON.parse(req.bodyRaw);
  log(parsedBodyRaw.blah)
  log(JSON.stringify(parsedBodyRaw.blah))
}
Drake
20 Sep, 2023, 22:44

what are you sending to the function? how are you executing your function? what is req.body?

RedLeeder
20 Sep, 2023, 23:22

Executing via manual execution in Appwrite Console as well as REST

Sending:

TypeScript
{ "myKey": "myValue" }

Function Code

TypeScript
export default async ({ req, res, log, error }) => {
  // req.body
  log("req.body");
  log(req.body);

  log("req.body.myKey");
  log(req.body.myKey);

  log("JSON.stringify(req.body.myKey)");
  log(JSON.stringify(req.body.myKey));

  // req.bodyRaw
  let parsedBodyRaw = JSON.parse(req.bodyRaw);
  log("parsedBodyRaw");
  log(parsedBodyRaw);

  log("parsedBodyRaw.myKey");
  log(parsedBodyRaw.myKey);

  log("JSON.stringify(parsedBodyRaw.myKey)");
  log(JSON.stringify(parsedBodyRaw.myKey));

Output

TypeScript
req.body
{ "myKey": "myValue" }

req.body.myKey
undefined

JSON.stringify(req.body.myKey)
undefined

------------

parsedBodyRaw
{"myKey":"myValue"}

parsedBodyRaw.myKey
myValue

JSON.stringify(parsedBodyRaw.myKey)
"myValue"

Did I find a bug or am I big dumb idiot of the day? 🀞 😨

Drake
20 Sep, 2023, 23:27

so it looks like req.body will only be parsed if the header specifies the content type is application/json:

https://github.com/open-runtimes/open-runtimes/blob/526741b4156d3983caf6d199c3761461101f6c6a/runtimes/node-18.0/src/server.js#L30-L32

RedLeeder
20 Sep, 2023, 23:32

Would you look at that...I might be biased but I would assume that JSON would be the default, especially with how the documentation and helper formats are all JSON

Drake
20 Sep, 2023, 23:33

ya....that would make sense...

mc-stephen
25 Sep, 2023, 08:59

Wow, didn't knew that

mc-stephen
25 Sep, 2023, 08:59

Taught so too

mc-stephen
30 Sep, 2023, 22:05

[SOLVED] Can't get body data in the req module for node.js

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