How to save a stream of bytes in a file from an API Call?

Hello,

I have an API containing a JSON object with a stream of bytes, which I can’t manage to save.
Do you have an example of how I can save this stream of bytes in a file in a workflow ?

The error I have is Expected a string, but got a object (original data: {"type":"Buffer","data":

Thanks !

When you say ‘stream of bytes’, what is the exact data type that you are receiving?

My understanding is that json does not support binary data, and the general practice is to use a base64 encoded string representing your binary data. The base64 encoded string can be saved to the bubble database and then retrieved and decoded to binary at the point of use (such as by using the JavaScript method for base64 decoding - I cant remember it off the top of my head but if you search for it, it will come right up, W3 schools has an tutorial on it). There are also methods for using base64 encoded data directly without manually such as with media URIs:

https://iandevlin.com/blog/2012/09/html5/html5-media-and-data-uri/

If you tell me a little more about how you are receiving and looking to use the data, and the data type, I may be able to walk you through it.

1 Like

Thanks @ryanellman.

Here is a bit more context.

In the plugin I am developing, I have created an action that returns a custom JSON object, which contains this stream of bytes which is a MP3, and it caused the error message I have pasted before in the app.

The flow is App button > Server Side Action with Custom Response Object > App response parsing.

The good news is that by encoding this stream of bytes in base64 before returning the JSON object in the SSA, now the app doesn’t crash anymore, but is parsing the base64 string as…base64 string, despite the type properly set in the custom type:
image

bubble.io seems to handle this value as a base64 string, and the :saved to S3 method doesn’t do anything.

I am able to successfully save this base64 string to the DB but it is not considered as a file.

How can I make bubble understand that tis base64 string is a file ?

No problem. So that error message was from trying to pass binary data to a JSON object?

That’s strange because I did that how you did it there, and specified that the API response with the base64 encoded string should be treated as a file and bubble is considering it a file. The difference may come from the fact that I was using the API connector to get a response from an external API, whereas you are building your plugin from the ground up. I’m not sure internally how bubble handles API calls from custom plugins vs from the API connector (I’ve never developed a plugin myself).

The thing is, the response is a string even if you are treating it as a file. So unless there was some bubble behavior that is specific to ‘File’ data types that is unavailable to strings, you should still be able to use the string for most purposes. Were you looking to simply play the mp3 file? Because you can construct a data uri using the base64 encoded string as text and use it just like you would use the music file.

This may help show how to do that.

You are da man !

Thanks !

No problem!! Happy to help!

This topic was automatically closed after 70 days. New replies are no longer allowed.