Back

[Solved] res.body isn't json

  • 0
  • Functions
SenZi
1 Sep, 2023, 19:49

I am running a function

TypeScript
const payload = req.body;

    const userId = payload.userId;
    const secretToken = payload.secretToken;

    if (!userId || !secretToken) {
        return res.json({
            status: "failed",
            message: "Invalid Parameters",
            body: JSON.stringify(payload)
        });
    }

the key value seems to be received as a query string instead of json.

TL;DR
The user was experiencing an issue with res.body not being in JSON format. Another user suggested that the issue may be caused by using form data instead of JSON. The solution was to use x-www-form-url-encoded to get JSON data in the request body. One user also mentioned that in Postman, using raw and inputting the actual JSON object will work. The problem with the user's code was that it was receiving the key value as a query string instead of JSON. The solution was to modify the code to parse the JSON payload correctly.
D5
1 Sep, 2023, 19:51

Aren't you using stringfy?

SenZi
1 Sep, 2023, 19:57

yea but it shouldn't make it a query string right with the =

Drake
1 Sep, 2023, 21:06

nope. you see you have x-www-form-url-encoded. that's the result when you do that

SenZi
1 Sep, 2023, 21:07

what should it be then?

Drake
1 Sep, 2023, 21:26

in Postman you have to use raw and put in the actual JSON object

SenZi
1 Sep, 2023, 21:59

Ohk Got It, That fixed it and it works fine. When i used to work with express js before i remember using the x-www-form-url-encoded to get json data in the request body.

VincentGe
1 Sep, 2023, 22:07

[Solved] res.body isn't json

Drake
1 Sep, 2023, 22:10

nope that's form data which is not json. express may have automatically been parsing the form data for you

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