Back

[SOLVED] Function response with base64 image

  • 0
  • Self Hosted
  • Functions
  • Web
AndrewIAm
16 Feb, 2023, 06:46

Hi. I'm trying to make a function which get image on 3rd party service and send it back to the user in base64 string. And issue seems like base64 string brakes response object. Appwrite runs locally on MacOS.

My functions return response:

res.json({ success: true, imageBase64: 'base-64-string'});

client side handler:

TypeScript
if (execution.statusCode !== 500) {
      console.log('response ::: ', execution.response.image)
      setImageData(execution.response.image);
 }

and console log output is:

{"success":true,"image":"iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAIAAAB7GkOtAAAAaGVYSWZNTQAqAAAACAACknwAAgAAACkAAAAmkoYAAgAAABgAAABQAAAAAE9wZW5BSS0tZjE4ZWQ5NzYxYjE2NW... Show more (1Mb) Copy

and can not be parsed. On client side I used React.js if it matters.

In case return object doesn't contain so long string as base65 image it parsed fine and I can get access to values by key in JS.

Any suggestion will be really appreciated.

TL;DR
The user is experiencing an issue where the base64 string in their function response breaks the response object. The response is a string, not an object. The limitation for large responses is 4MB, so the data might be truncated. The solution is to use the storage API for files instead of sending the base64 string in the response object.
Drake
16 Feb, 2023, 18:29

Btw, response is a string, not object 🧐

AndrewIAm
16 Feb, 2023, 19:29

Probably yes, but I can get access to for example, response.success in case when return not so much data

AndrewIAm
16 Feb, 2023, 19:53

Btw, if it returns a string why there are 2 types of response: res.send and res.json?

Drake
16 Feb, 2023, 19:54

On the open runtimes (function side) res.json will automatically serialize. Appwrite side does not automatically deserialize

Drake
16 Feb, 2023, 19:55

You can get the response.success after parsing the string with like JSON.parse, right?

AndrewIAm
16 Feb, 2023, 20:14

Sure, just wonder why I have not to do this if response body short so I can access it directly without deserialization. But it my case I’ve tried to parse response and no success. Message was regarding unexpected string end or something like that. I’m assuming it’s because of long base64 string, more than 10000 characters

Drake
16 Feb, 2023, 20:16

What version of Appwrite are you on? You should need to deserialize it if it's working successfully 🧐

Large responses don't work for functions. There's a limit of 4MB or something so your data might be truncated. For files, you should really use the storage API

AndrewIAm
16 Feb, 2023, 20:19

I have to check if my response is more than 4Mb. Do you have the exact limitation?

AndrewIAm
16 Feb, 2023, 20:24

I’ll check sizes of my files then and get back later

AndrewIAm
17 Feb, 2023, 05:49

@Steven you are right, my files are 2Mb average so sending base64 withing responss object not a good idea. Thanks for the clue. And I belive this limitation should be highlited in the docs.

Drake
17 Feb, 2023, 05:54

It's documented here but it's outdated: https://appwrite.io/docs/models/execution. The limit used to be smaller

AndrewIAm
17 Feb, 2023, 05:57

Ahh, missed it. Gone through Guides section and missed it 🧐

VincentGe
17 Feb, 2023, 15:15

😬 What is it now? Lemme go update

Drake
17 Feb, 2023, 15:17

1mb, but will probably change in the next release

AndrewIAm
19 Feb, 2023, 08:35

[RESOLVED] Function response with base64 image

VincentGe
20 Feb, 2023, 19:47

[SOLVED] Function response with base64 image

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