How to send data with attachment in Form Data multipart

I am calling an API where I need to pass the request in formData. It is working fine with normal request parameters, but when I am adding attachments in the “Name” field of body parameter and file names in the “value” field then it is throwing an error.

Please share your workflow

Share the output returned by the last node

when I am adding attachment in the body parameter as below , it is throwing an error:

the whole request should be in the below format:

Kindly help me as I am stuck here. Thanks!

Hi @Farheen_I, unfortunately you didn’t provide the details requested in the template specify. However, based on the nodes you are using I assume it’s an older version of n8n. So in a first step, I’d suggest you update upgrade to the latest version of n8n.

Then add a new HTTP Request node from scratch.

The latest version of the HTTP Request node will make it possible to mix binary data with other form data:

Or in a workflow:

Using a service such as webhook.site you can see both the form values as well as the binary file are submitted as expected:

Hope this helps :slight_smile:

Thank you, @MutedJam. I will give it a shot and let you know if there are any problems.

1 Like

Hi @MutedJam , I tried with the new updated version of n8n. But in the binary name field, I am not able to understand how to pass the value on it. I am sending you the previous node execution where I am getting an image in the field of attachment.

Please help me how to pass the image from data object to the binary field.

Hi @MutedJam can you help me on this. Thanks!

Hi @Farheen_I, the value would have to match the name of your binary property like so:

Your example Code node only returns JSON data though, do you not have any binary data? Perhaps you can share a screenshot of the “Binary” tab on your input data?

Hello, @MutedJam I do not specifically have a node that returns binary data. The current code node returns a single value called “attachments,” which I must pass inside of formData.
sharing with you two screenshots, one showing you how I must send the attachment data using Postman.
The second screenshot is the code node- which is how I am getting this data from an Api.

Please let me know how can i use this in my formData of http node.


Hi @Farheen_I, I am not using Postman. Can you export your Postman request using the curl format and share it (make sure to redact all confidential values such as API keys etc) as text rather than a screenshot?

@MutedJam Here it is,
curl --location ‘https://shalimarpaints.freshservice.com/api/v2/tickets
–header ‘Authorization: Basic [REDACTED]’
–form ‘attachments[]=@“/C:/Users/Wasif Khan/Desktop/ticket.curl”’
–form ‘description=“Details about the issue…”’
–form ‘subject=“Support Needed…”’
–form ‘custom_fields[complaints]=“Brush Mark”’

So it looks like this requests reads data from your hard drive. To achieve this you’d need to use the Read Binary Files node. This provide give you a binary item in n8n:

Keep in mind this node reads data from the hard drive of the server running n8n, which might not be the machine you are accessing n8n on.

Hello, @MutedJam I believe our conversation is moving in a different direction; perhaps I am not making myself clear. I am sorry if I caused any confusion earlier. I am making it much simpler to read.
I would like to send all the data from the code node in the formData format. Specifically, I want to send the “attachment” attribute as binary data.

Please let me know how I can achieve this. Once more, I am sending you a screenshot of the code node and the http node, where I have added a binary parameter and a key as an “attachment[]”. However, the main issue is what I should add to its value. I want to pass the value of “attachment”, which I am getting from my previous node, i.e., the code node.

{
“meta”: {
“instanceId”: “f85288737922f7456a98dabca1d0c61e25f3b046c522ce3d3cb84d81ec321dd7”
},
“nodes”: [
{
“parameters”: {
“method”: “POST”,
“url”: “https://shalimarpaints.freshservice.com/api/v2/tickets”,
“authentication”: “genericCredentialType”,
“genericAuthType”: “httpBasicAuth”,
“sendBody”: true,
“contentType”: “multipart-form-data”,
“bodyParameters”: {
“parameters”: [
{
“name”: “description”,
“value”: “={{ $node["mapping data"].json["data"]["description"] }}”
},
{
“name”: “subject”,
“value”: “={{ $node["mapping data"].json["data"]["subject"] }}”
},
{
“name”: “email”,
“value”: “={{ $node["mapping data"].json["data"]["email"] }}”
},
{
“name”: “priority”,
“value”: “={{ $json.data.priority }}”
},
{
“name”: “status”,
“value”: “={{ $json.data.status }}”
},
{
“name”: “workspace_id”,
“value”: “={{ $json.data.workspace_id }}”
},
{
“name”: “custom_fields[lf_ticket_department]”,
“value”: “={{ $json.data.custom_fields.lf_ticket_department }}”
},
{
“name”: “custom_fields[complaints]”,
“value”: “={{ $json.data.custom_fields.complaints }}”
},
{
“name”: “=custom_fields[item_type]”,
“value”: “={{ $json.data.custom_fields.item_type }}”
},
{
“name”: “custom_fields[product_code]”,
“value”: “={{ $json.data.custom_fields.product_code }}”
},
{
“name”: “custom_fields[product_code]”,
“value”: “={{ $json.data.custom_fields.product_code }}”
},
{
“name”: “custom_fields[product_name]”,
“value”: “={{ $json.data.custom_fields.product_name }}”
},
{
“name”: “custom_fields[customer_code]”,
“value”: “={{ $json.data.custom_fields.customer_code }}”
},
{
“name”: “custom_fields[customer_name]”,
“value”: “={{ $json.data.custom_fields.customer_name }}”
},
{
“name”: “custom_fields[beatroute_id]”,
“value”: “={{ $json.data.custom_fields.beatroute_id }}”
},
{
“name”: “custom_fields[batch_number]”,
“value”: “={{ $json.data.custom_fields.batch_number }}”
},
{
“name”: “custom_fields[manufacturing_date]”,
“value”: “={{ $json.data.custom_fields.manufacturing_date }}”
},
{
“name”: “custom_fields[damaged_quantity]”,
“value”: “={{ $json.data.custom_fields.damaged_quantity }}”
},
{
“name”: “custom_fields[retailer_br_id]”,
“value”: “={{ $json.data.custom_fields.retailer_br_id }}”
},
{
“name”: “custom_fields[beatroute_id]”,
“value”: “={{ $json.data.custom_fields.beatroute_id }}”
},
{
“name”: “custom_fields[item_type]”,
“value”: “={{ $json.data.custom_fields.item_type }}”
},
{
“name”: “custom_fields[lf_ticket_department]”,
“value”: “={{ $json.data.custom_fields.lf_ticket_department }}”
},
{
“name”: “cc_emails”,
“value”: “={{ $json.data.cc_emails }}”
},
{
“parameterType”: “formBinaryData”,
“name”: “attachments[]”
}
]
},
“options”: {}
},
“id”: “1628770e-b321-481b-8307-be5ccc499448”,
“name”: “HTTP Request”,
“type”: “n8n-nodes-base.httpRequest”,
“typeVersion”: 4.1,
“position”: [
-120,
940
],
“credentials”: {
“httpBasicAuth”: {
“id”: “117”,
“name”: “Freshservice”
}
}
},
{
“parameters”: {
“jsCode”: “var data = {\n "data": {\n "subject": "Brush Mark",\n "attachments": "20230630_1688106262392.jpg",\n "description": "test",\n "email": "[email protected]",\n "cc_emails": [],\n "priority": 2,\n "workspace_id": 2,\n "status": 2,\n "custom_fields": {\n "lf_ticket_department": "27000440761",\n "item_type": "Decorative",\n "complaints": "Brush Mark",\n "product_code": "50004054020000",\n "product_name": "THINNER FOR ANTIBRINE AL.THIN.6754 INDL P C 20 LT",\n "batch_number": "check",\n "manufacturing_date": "2023-06-30",\n "damaged_quantity": "1",\n "customer_code": "E404O04401",\n "customer_name": "Apurwatest-Dealer",\n "beatroute_id": "8051",\n "retailer_br_id": "5638431"\n }\n }}\n\nreturn data”
},
“id”: “78c2fa4d-3b78-46db-a309-844ec14aa251”,
“name”: “Code”,
“type”: “n8n-nodes-base.code”,
“typeVersion”: 1,
“position”: [
-380,
960
]
}
],
“connections”: {
“Code”: {
“main”: [
[
{
“node”: “HTTP Request”,
“type”: “main”,
“index”: 0
}
]
]
}
}
}

Specifically, I want to send the “attachment” attribute as binary data.

So you simply want to write the string 20230630_1688106262392.jpg from the attachments field of your example data into a binary file and then send it in the attachments[] field? It seems rather unusual for any API to require a string in this format, but you could create such a file using the Move Binary Data node:

Or do you want to send an actual image? If so, where would you get this image from?

This solution worked for me.Thanks! :grinning:

2 Likes

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