Back

[SOLVED] responseBody does not contain a valid JSON in appwrite function

  • 0
  • Functions
  • Flutter
THE-E
23 Sep, 2023, 18:12

I am currently facing an issue after upgrading to 1.4.3 regarding the responseBody when returning JSON from the function.

So my Node.js function has the following lines:

TypeScript
      log(JSON.stringify(myContainers));
      return res.json(JSON.stringify(myContainers));

The appwrite log output show the following (as desired) value:

TypeScript
[{"_id":"649808696bca89048c8f0653","name":"Rombergstraße 118","label":"Weihnachtsdeko","uuid":"6a17cb1b-4215-4538-a2ae-0ef8a0af7fc8","location":"Unbekannt","description":"","tags":["test"],"owners":[{"username":"testuser@example.com","name":"Test User","userId":"649807cb6488dc8ff3c5"}],"readUsers":[],"rfid":""},{"_id":"649808696bca89048c8f0654","name":"Bethmannstraße 115","label":"Behälter 2","uuid":"e97b24c3-df68-45ef-a7dd-c830205cfae0","location":"Unbekannt","tags":["test"],"owners":[{"username":"testuser@example.com","name":"Test User","userId":"649807cb6488dc8ff3c5"}],"readUsers":[]}]

But the responseBody of the request, when using Postman returns:

TypeScript
"responseBody": "\"[{\\\"_id\\\":\\\"649808696bca89048c8f0653\\\",\\\"name\\\":\\\"Rombergstraße 118\\\",\\\"label\\\":\\\"Weihnachtsdeko\\\",\\\"uuid\\\":\\\"6a17cb1b-4215-4538-a2ae-0ef8a0af7fc8\\\",\\\"location\\\":\\\"Unbekannt\\\",\\\"description\\\":\\\"\\\",\\\"tags\\\":[\\\"test\\\"],\\\"owners\\\":[{\\\"username\\\":\\\"testuser@atic.ar\\\",\\\"name\\\":\\\"Test User\\\",\\\"userId\\\":\\\"649807cb6488dc8ff3c5\\\"}],\\\"readUsers\\\":[],\\\"rfid\\\":\\\"\\\"},{\\\"_id\\\":\\\"649808696bca89048c8f0654\\\",\\\"name\\\":\\\"Bethmannstraße 115\\\",\\\"label\\\":\\\"Behälter 2\\\",\\\"uuid\\\":\\\"e97b24c3-df68-45ef-a7dd-c830205cfae0\\\",\\\"location\\\":\\\"Unbekannt\\\",\\\"tags\\\":[\\\"test\\\"],\\\"owners\\\":[{\\\"username\\\":\\\"testuser@atic.ar\\\",\\\"name\\\":\\\"Test User\\\",\\\"userId\\\":\\\"649807cb6488dc8ff3c5\\\"}],\\\"readUsers\\\":[]}]\"",

The problem is: the response is surrounded with additional " which are escaped (\").

TL;DR
The issue is that the `responseBody` is surrounded by additional quotation marks that are escaped, causing the `jsonDecode` function in Flutter to interpret it as a string instead of a dynamic object. The solution is to change `return res.json(JSON.stringify(testObject));` to `return res.json(testObject);` in the TypeScript file.
THE-E
23 Sep, 2023, 18:15

The issue is, in my Flutter code the jsonDecode function interprets the value of the reponseBody as a string, instead of an dynamic object.

The value I have copied is directly from Postman to have a more RAW response and out rule package issues from Flutter.

THE-E
23 Sep, 2023, 19:04

So I created a minimum example:

TypeScript file

TypeScript
type Context = {
  req: any;
  res: any;
  log: (msg: any) => void;
  error: (msg: any) => void;
};

class TestModel {
  constructor(firstname: string, lastname: string){
    this.firstname = firstname;
    this.lastname = lastname;
  }
  firstname: string;
  lastname: string;
};

export default async ({ req, res, log, error }: Context) => {
  let testObject = new TestModel("John", "Doe");

  return res.json(JSON.stringify(testObject));
};

The responseBody is:

TypeScript
{
...
"responseBody": "\"{\\\"firstname\\\":\\\"John\\\",\\\"lastname\\\":\\\"Doe\\\"}\"",
...
}
THE-E
23 Sep, 2023, 19:12

Alright I think I understood the mistake. The res.json function changed in such a way, that the json.stringify() is done implicitly. So changing ...

TypeScript
res.json(JSON.stringify(testObject));

to ...

TypeScript
res.json(testObject);

should do the trick. 🙂

Drake
23 Sep, 2023, 21:38

You shouldn't have even been doing res.json(JSON.stringify(testObject)) in the previous versions 👀

Drake
23 Sep, 2023, 21:43

[SOLVED] responseBody does not contain a valid JSON in appwrite function

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