Download binary file

how can I download an audio file, that is linked in a webhook and then send the content of the file again in another node via an http request?

Hi @inop, if the file is linked in your webhook through a URL you’d need two separate HTTP requests for the case you have described: The first one to download the file from the URL you receive in your webhook, the second one to send it to your destination.

Assuming you are receiving a webhook request like this:

curl --request POST \
  --url https://your.n8n.url/webhook-test/022076cd-0925-4f85-9bc6-b5bdc0f354f2 \
  --header 'Content-Type: application/json' \
  --data '{
	"file_url": "https://upload.wikimedia.org/wikipedia/commons/4/45/Ode_to_Joy_-_Concert_Band_-_United_States_Air_Force_Band_of_the_Rockies.mp3"
}'

The workflow could look like so:

The important part here is that you pick Form-Data as the Body Content Type when uploading a file and specify the correct binary property so n8n can identify the file to upload:

Afterwards the file should be uploaded to your destination as expected. In the example workflow I’ve used a URL provided by webhook.site, making this easy to verify as it shows the full request it has received:

Hope this helps!

2 Likes

that’s already very helpful, thanks!
One more question: I need to forward the file like this

  "content": {
    "mimetype": "image/jpeg",
    "data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=",
    "filename": "image.jpg"

How exactly do I go about that? Sorry, quite new to all of this

Hi @inop, this looks like you might receive base64 encoded data in your webhook (although there appears to be a mismatch between the actual file content and the mime type - the data looks like a PNG image rather than JPEG).

This means you wouldn’t need the first HTTP Request node from the example as n8n already receives the image data in the webhook. Assuming you are sending a webhook like this:

curl --request POST \
  --url https://your.n8n.url/webhook-test/022076cd-0925-4f85-9bc6-b5bdc0f354f2 \
  --header 'Content-Type: application/json' \
  --data '{
  "content": {
    "mimetype": "image/png",
    "data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=",
    "filename": "image.png"
  }
}'

Unfortunately, n8n currently has an open bug preventing the conversion of such data through the “Convert to/from binary data” node, but you can still process it with a little bit of custom code returning a suitable n8n data structure. Here’s a quick workflow reading such data and transforming it into an n8n binary item before uploading it:

Perhaps you can give this a go on your end and confirm if this works for you?

Thanks a lot once again.

I think I described my problem inaccurately. The webhook just gives me the link to the file, which I then download with a second http request, as you explained. However, I then need to send this file to another api, in the format (base64 encoded it seems) I showed above. How exactly do I get the data object I downloaded into such a format so that I can then put it there?

I’m so sorry, I thought you were receiving base 64 rather than sending it. Sorry for misreading :frowning:

Anyway, back to the original webhook:

curl --request POST \
  --url https://your.n8n.url/webhook-test/022076cd-0925-4f85-9bc6-b5bdc0f354f2 \
  --header 'Content-Type: application/json' \
  --data '{
	"file_url": "https://upload.wikimedia.org/wikipedia/commons/4/45/Ode_to_Joy_-_Concert_Band_-_United_States_Air_Force_Band_of_the_Rockies.mp3"
}'

This can be done pretty much with my first example, all that’s needed is creating the base64 string and adjusting your HTTP Request accordingly. This is how the complete workflow could look like:

Hope this one helps :slight_smile:

1 Like

Okay, almost there.

Everything seems to be working, one final issue: When drag the data into the upload node, it shows up red like this
image
If I click on exectue anyways, I get error 413, despite the file only being 7mb aaand I changed the max payload env to 30. Is it possible that this occurs because the node gets executed multiple times (there’s another variable that includes multiple destinations for the http request, so it runs multiple times) and every execution adds 7mb to the payload or something?

Hi @inop, I am afraid I don’t quite know why the preview would show up in red. I suspect this could be a combination of the exact n8n version and the amount of data you have in use. As long as your base64 data lives in a field named data the expression should be good though

Perhaps you can confirm your n8n version, share your workflow and the exact data you are sending to the HTTP Request node handling the upload so I can reproduce this?

With the 413 issue, where exactly are you seeing this? It might not be thrown by n8n directly, but by a reverse proxy server you are using. With nginx for example you might need to set client_max_body_size to a higher value: Module ngx_http_core_module

ahem. Back at this.
so I tried it again, this time the docker container crashed. here’s the logs:

Press "o" to open in Browser.
PayloadTooLargeError: request entity too large
    at readStream (/usr/local/lib/node_modules/n8n/node_modules/raw-body/index.js:163:17)
    at executor (/usr/local/lib/node_modules/n8n/node_modules/raw-body/index.js:120:5)
    at new Promise (<anonymous>)
    at getRawBody (/usr/local/lib/node_modules/n8n/node_modules/raw-body/index.js:119:10)
    at IncomingMessage.req.readRawBody (/usr/local/lib/node_modules/n8n/dist/middlewares/bodyParser.js:39:56)
    at parseBody (/usr/local/lib/node_modules/n8n/dist/middlewares/bodyParser.js:50:15)
    at bodyParser (/usr/local/lib/node_modules/n8n/dist/middlewares/bodyParser.js:76:33)
    at newFn (/usr/local/lib/node_modules/n8n/node_modules/express-async-errors/index.js:16:20)
    at Layer.handle [as handle_request] (/usr/local/lib/node_modules/n8n/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/usr/local/lib/node_modules/n8n/node_modules/express/lib/router/index.js:328:13)
PayloadTooLargeError: request entity too large
    at readStream (/usr/local/lib/node_modules/n8n/node_modules/raw-body/index.js:163:17)
    at executor (/usr/local/lib/node_modules/n8n/node_modules/raw-body/index.js:120:5)
    at new Promise (<anonymous>)
    at getRawBody (/usr/local/lib/node_modules/n8n/node_modules/raw-body/index.js:119:10)
    at IncomingMessage.req.readRawBody (/usr/local/lib/node_modules/n8n/dist/middlewares/bodyParser.js:39:56)
    at parseBody (/usr/local/lib/node_modules/n8n/dist/middlewares/bodyParser.js:50:15)
    at bodyParser (/usr/local/lib/node_modules/n8n/dist/middlewares/bodyParser.js:76:33)
    at newFn (/usr/local/lib/node_modules/n8n/node_modules/express-async-errors/index.js:16:20)
    at Layer.handle [as handle_request] (/usr/local/lib/node_modules/n8n/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/usr/local/lib/node_modules/n8n/node_modules/express/lib/router/index.js:328:13)
PayloadTooLargeError: request entity too large
    at readStream (/usr/local/lib/node_modules/n8n/node_modules/raw-body/index.js:163:17)
    at executor (/usr/local/lib/node_modules/n8n/node_modules/raw-body/index.js:120:5)
    at new Promise (<anonymous>)
    at getRawBody (/usr/local/lib/node_modules/n8n/node_modules/raw-body/index.js:119:10)
    at IncomingMessage.req.readRawBody (/usr/local/lib/node_modules/n8n/dist/middlewares/bodyParser.js:39:56)
    at parseBody (/usr/local/lib/node_modules/n8n/dist/middlewares/bodyParser.js:50:15)
    at bodyParser (/usr/local/lib/node_modules/n8n/dist/middlewares/bodyParser.js:76:33)
    at newFn (/usr/local/lib/node_modules/n8n/node_modules/express-async-errors/index.js:16:20)
    at Layer.handle [as handle_request] (/usr/local/lib/node_modules/n8n/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/usr/local/lib/node_modules/n8n/node_modules/express/lib/router/index.js:328:13)

<--- Last few GCs --->

[7:0x7fea97730030] 855586886 ms: Scavenge (reduce) 2044.5 (2052.1) -> 2044.4 (2053.4) MB, 2.3 / 0.0 ms  (average mu = 0.393, current mu = 0.348) allocation failure; 
[7:0x7fea97730030] 855587001 ms: Mark-sweep (reduce) 2045.6 (2053.4) -> 2045.3 (2054.4) MB, 9.8 / 0.0 ms  (+ 84.7 ms in 17 steps since start of marking, biggest step 9.6 ms, walltime since start of marking 97 ms) (average mu = 0.326, current mu = 0.234) a

<--- JS stacktrace --->

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
Last session crashed
Initializing n8n process
n8n ready on 0.0.0.0, port 5678
Found unfinished executions: 114
This could be due to a crash of an active workflow or a restart of n8n.
Currently active workflows:
   - Memo-sender (ID: LK8VsDg7lL1ZEVNk)
Attempting to recover execution 114
Version: 1.14.0
 ================================
   Start Active Workflows:
 ================================
   - Memo-sender (ID: LK8VsDg7lL1ZEVNk)
     => Started

Editor is now accessible via:
http://localhost:5678/

Press "o" to open in Browser.

<--- Last few GCs --->

[7:0x7fdf8a203030]  1065253 ms: Scavenge (reduce) 2038.8 (2046.9) -> 2038.7 (2048.1) MB, 1.8 / 0.0 ms  (average mu = 0.501, current mu = 0.229) allocation failure; 
[7:0x7fdf8a203030]  1065334 ms: Mark-sweep (reduce) 2061.0 (2070.0) -> 2060.8 (2070.2) MB, 41.5 / 0.0 ms  (+ 35.5 ms in 70 steps since start of marking, biggest step 20.3 ms, walltime since start of marking 131 ms) (average mu = 0.536, current mu = 0.558)

<--- JS stacktrace --->

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

the workflow looks like this:

The mp3 gets downloaded, converted into json (aso you showed me), then multiple http requests are made based for multiple entries in a database (over a hundred). Notably, not one of the final requests gets sent correctly, the crash occurs when clicking on execute workflow.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.