How to send binary data in body HTTP POST

Hi all :smiley:
I am moving one of my scenarios from to n8n. Basically I have to download a file from one platform via HTTP Get request and then upload it to another platform with HTTP Post request, but the file has to be in JSON body. I have managed to do it in where I just mapped “data” field (image 1), where data is passed as binary

image 1

When I download a file with GET request I can see the PDF, but when I try to upload it with POST request I only get metadata from file and no binary data.

Does anyone know how to send binary data in JSON?

Hi @anze.gracar, welcome to the community!

Sending binary data directly inside JSON would require some form of encoding. I am not familiar with make, but I suspect they might use base64 for this purpose (but it’s worth double-checking what exactly your destination API expects).

n8n would not do this automatically, but you can still generate a base64 string based on a file like so:

I am using a Function Item node which can run JS code to build the base64-encoded string for my example file which I am then sending to together with a simple human-readable string.

This is the data arriving on the upload server:

Hope this helps!

1 Like

ooh, thanks. I works :slight_smile: I tried converting it to base64 with Move binary data, but it did not work. It works with your code :slight_smile:

1 Like

Glad to hear, thanks so much for confirming!

This three liner is pretty much vanilla JS/Node.js btw, the only exception is the getBinaryDataBuffer method explained here in case you want to dive deeper into what’s happening here.