Build Form in a code Node and Post it inside an HTTP request node

Describe the problem/error/question

Hello,

I am having hard time lately trying to use an HTTP Request node to send a multipart/form-data payload . To make it understandable we could say the call I am trying to do is this kind :

 curl --location --request POST ‘$MY_URL”\
--header 'Authorization: Basic <AUTH>' \
—form 'payload= {"inputs": [       \
    { "type": "ATTACHMENT", "contentType": "file", "file_key": "file1" },   \
    { "type": "ATTACHMENT", "contentType": "file", "file_key": "file2" },   \
    { "type": "BODY", "contentType": "text", "text": "My blablabla text" } ]} ' \
--form 'file1=@"/Users/me/Downloads/FileA.png"' \
--form 'file2=@"/Users/me/Downloads/FileB.pdf"'

But the number of attachements may vary from 0 to N. And for each of them I need to append the form a new Key ‘fileX’ with the binary file as the value and app to my Json table behind key = ‘payload’ a new line “ { “type”: “ATTACHMENT”, “contentType”: “file”, “file_key”: “fileX” }”

X in [0 …N ]

Trying to do it with HTTP node only I can’t make a variable of the number of files as explained here : Pass several files with HTTP node - #5 by MutedJam

I finally resign myself to code it inside a code node using “axios” else “this.helpers.httpRequestWithAuthentication”

→ I wanted to know if there is a way to send my form from an HTTP node ? I don’t know how to set it up actually .

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.56.2
  • Database (default: SQLite): PgSQL
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Main
  • **Running n8n via (Docker, npm, n8n cloud, desktop app): docker **
  • Operating system: K8S

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hey , Sorry for the missing information . I just updated

Hi @William_Guerzeder,

If you want to upload multiple files to your endpoint where the number of files differs every time, the best solution would be to split up your files and send them as separate HTTP requests as suggested here:

This way, the number of files shouldn’t matter.

Also, I’m not sure what you mean here:

but you should be able to reference the item’s index number with file_{{ $itemIndex }}:

hope that helps!

2 Likes

Yes thank you , for your answer.

the thing is that i must put the N number of file in the same POST to have them handle by the system as one entity . which is what i need. => It won’t work if i do multiple calls .

So that’s why i need to append the files in one form. (as i do in my code node)

when i say : “I can’t make a variable of the number of files”
it means i can do using an HTTP node to build a form from input files, but can’t have the number of file in the input unknown . This need to be a fixed number of file .
so i could switch case if 1 file , if 2 files , if 3 files etc … but then will have a much number of similar node to maintain .

My idea was to use the Code node to build the form . Then i wanted to pass the built form to an http node to handle “creds, http calls, retry , error …” that it does pretty well.

But i don’t understand how i can pass that form from the code node to the http node.

Thanks for your attention, I love n8n
Regards

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