# Multipart data (json +file)

**URL:** <https://discourse.nodered.org/t/multipart-data-json-file/48134>\
**Category:** General\
**Created:** [7 July 2021 12:24 UTC](https://discourse.nodered.org/t/multipart-data-json-file/48134 "2021-07-07T12:24:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yupadhye](https://avatars.discourse-cdn.com/v4/letter/y/f14d63/32.png) [@yupadhye](https://discourse.nodered.org/u/yupadhye)\
**Post date:** [7 July 2021 12:24 UTC](https://discourse.nodered.org/t/multipart-data-json-file/48134/1 "2021-07-07T12:24:47Z")

</div>

Hello  
I want to send HTTP post request that has json + file upload. Can I do it in one shot? Or I have to send json first and then attachment (file)?

Can someone please guide?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [7 July 2021 12:31 UTC](https://discourse.nodered.org/t/multipart-data-json-file/48134/2 "2021-07-07T12:31:09Z")

</div>

Search the forum for multipart.

Here is one such example : [Form-data multipart-encoded request with pdf file attached into the body](https://discourse.nodered.org/t/form-data-multipart-encoded-request-with-pdf-file-attached-into-the-body/44749)

---

<div class="post-metadata">

**Author:** ![yupadhye](https://avatars.discourse-cdn.com/v4/letter/y/f14d63/32.png) [@yupadhye](https://discourse.nodered.org/u/yupadhye)\
**Post date:** [7 July 2021 14:06 UTC](https://discourse.nodered.org/t/multipart-data-json-file/48134/3 "2021-07-07T14:06:36Z")

</div>

`I have referred the page you have recommended. Can you please tell me whats going wrong?

[{"id":"a12f24af.f30b98","type":"function","z":"5b4e4e9f.708f4","name":"","func":"msg.headers = {\n 'content-type' : 'multipart/form-data'\n}\n\nmsg.payload = {\n file: {\n field: 'file',\n data: msg.payload.byteArray,\n type: 'binary',\n name: msg.payload.fname\n },\n formOptions: {\n ideaName: 'idea1',\n problemDescription:'problem1'\n }\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":,"x":370,"y":340,"wires":[["412c3b14.67b054"]]}]

---

<div class="post-metadata">

**Author:** ![yupadhye](https://avatars.discourse-cdn.com/v4/letter/y/f14d63/32.png) [@yupadhye](https://discourse.nodered.org/u/yupadhye)\
**Post date:** [8 July 2021 09:43 UTC](https://discourse.nodered.org/t/multipart-data-json-file/48134/4 "2021-07-08T09:43:26Z")

</div>

I am using fileinject which passes msg.payload to a function. My function looks like this

var fileData = msg.payload;  
var msg1,msg2

var data={ "ideaName": "idea1",  
"problemDescription":"test"};  
msg.headers = {'Content-Type': 'multipart/form-data'};

msg1 = {payload: {JSONString: JSON.stringify(data)}} ;

msg2={payload:{

```
'image' : {
    'value': fileData,
    'options': {
        'filename': 'mytest.txt'
    }
}

```

}

}

return[msg,[msg1,msg2]];

But this sends only file paylod to HTTP request.

Any guidance?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 July 2021 11:02 UTC](https://discourse.nodered.org/t/multipart-data-json-file/48134/5 "2021-07-08T11:02:22Z")

</div>

That won't be multipart.

Check this other thread (that I found by searching **multipart** )...

> [@Send a file with multipart/form-data to OctoPrint REST API](https://discourse.nodered.org/t/send-a-file-with-multipart-form-data-to-octoprint-rest-api/7534/15):
>
> That isn't 100% true. The HTTP In node does support file uploads now - select Post and tick the box to enable file uploads. To send a multipart upload from the HTTP Request node is more work, but doable. Here's a Function that will construct a suitable message to pass to the HTTP Request node: msg.headers = { "Content-Type": "multipart/form-data; boundary=------------------------d74496d66958873e" } msg.payload = '--------------------------d74496d66958873e\r\n'+ 'Content-Disposition: fo…

---

<div class="post-metadata">

**Author:** ![yupadhye](https://avatars.discourse-cdn.com/v4/letter/y/f14d63/32.png) [@yupadhye](https://discourse.nodered.org/u/yupadhye)\
**Post date:** [8 July 2021 14:37 UTC](https://discourse.nodered.org/t/multipart-data-json-file/48134/6 "2021-07-08T14:37:28Z")

</div>

thanks!  
[{"id":"5b4e4e9f.708f4","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"9c0b2081.b1ac6","type":"function","z":"5b4e4e9f.708f4","name":"Format the header and payload","func":"msg.headers = {\n "Content-Type": "multipart/form-data; boundary=------------------------d74496d66958873e"\n}\n\nmsg.payload = '--------------------------d74496d66958873e\r\n'+\n'Content-Disposition: form-data; name="ideaName"\r\n'+\n'\r\n'+\n'idea1\r\n'+\n'--------------------------d74496d66958873e\r\n'+\n'Content-Disposition: form-data; name="problemDescription"\r\n'+\n'\r\n'+\n'test\r\n'+\n'--------------------------d74496d66958873e\r\n'+\n'Content-Disposition: form-data; name="file"; filename="/C:/Users/Yogini/Downloads/VAST\_2021\_Holiday List (1).pdf"\r\n'+\n'Content-Type: text/plain\r\n'+\n'\r\n'+\nmsg.payload+'\r\n'+\n'--------------------------d74496d66958873e--\r\n';\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":,"x":310,"y":540,"wires":[["85c748e8.2d1f88"]]},{"id":"85c748e8.2d1f88","type":"http request","z":"5b4e4e9f.708f4","name":"","method":"POST","ret":"obj","paytoqs":"ignore","url":"[https://prod-102.westus.logic.azure.com:443/workflows/13a1c2517dea4c10b56508b83069102a/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=pK7xXXMjndVz0Kfdp4glDimhKgFscAhZBqlvSGKd9K4","tls":"","persist":false,"proxy":"","authType":"","x":550,"y":540,"wires":[["a4810d7a.f652a"]]},{"id":"a4810d7a.f652a","type":"debug","z":"5b4e4e9f.708f4","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":520,"wires":[]},{"id":"ed2d4050.7a30e","type":"fileinject","z":"5b4e4e9f.708f4","name":"","x":170,"y":380,"wires":[["9c0b2081.b1ac6](https://prod-102.westus.logic.azure.com:443/workflows/13a1c2517dea4c10b56508b83069102a/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=pK7xXXMjndVz0Kfdp4glDimhKgFscAhZBqlvSGKd9K4%22,%22tls%22:%22%22,%22persist%22:false,%22proxy%22:%22%22,%22authType%22:%22%22,%22x%22:550,%22y%22:540,%22wires%22:%5B%5B%22a4810d7a.f652a%22%5D%5D%7D,%7B%22id%22:%22a4810d7a.f652a%22,%22type%22:%22debug%22,%22z%22:%225b4e4e9f.708f4%22,%22name%22:%22%22,%22active%22:true,%22tosidebar%22:true,%22console%22:true,%22tostatus%22:false,%22complete%22:%22true%22,%22targetType%22:%22full%22,%22statusVal%22:%22%22,%22statusType%22:%22auto%22,%22x%22:730,%22y%22:520,%22wires%22:%5B%5D%7D,%7B%22id%22:%22ed2d4050.7a30e%22,%22type%22:%22fileinject%22,%22z%22:%225b4e4e9f.708f4%22,%22name%22:%22%22,%22x%22:170,%22y%22:380,%22wires%22:%5B%5B%229c0b2081.b1ac6)"]]}]

I see file payload but dont see form data

{"payload":"","filename":"mytest.txt","mimetype":"text/plain","\_msgid":"2de3f8e8.177ba8","headers":{"cache-control":"no-cache","pragma":"no-cache","expires":"-1","x-ms-workflow-run-id":"08585758519470315704503619175CU102","x-ms-correlation-id":"8686c4a5-e433-4a4d-917c-aa0d25c226b1","x-ms-client-tracking-id":"08585758519470315704503619175CU102","x-ms-trigger-history-name":"08585758519470315704503619175CU102","x-ms-execution-location":"westus","x-ms-workflow-system-id":"/locations/westus/scaleunits/prod-102/workflows/13a1c2517dea4c10b56508b83069102a","x-ms-workflow-id":"13a1c2517dea4c10b56508b83069102a","x-ms-workflow-version":"08585776812655828111","x-ms-workflow-name":"d74a2bd5-7740-2aba-a189-dbf7642adc50","x-ms-tracking-id":"8686c4a5-e433-4a4d-917c-aa0d25c226b1","x-ms-ratelimit-burst-remaining-workflow-writes":"2998","x-ms-ratelimit-remaining-workflow-download-contentsize":"214748160","x-ms-ratelimit-remaining-workflow-upload-contentsize":"214747200","x-ms-ratelimit-time-remaining-directapirequests":"19999867","x-ms-request-id":"westus:8686c4a5-e433-4a4d-917c-aa0d25c226b1","strict-transport-security":"max-age=31536000; includeSubDomains","date":"Thu, 08 Jul 2021 14:35:38 GMT","connection":"close","content-length":"0","x-node-red-request-node":"30b94f73"},"statusCode":202,"responseUrl":"[https://prod-102.westus.logic.azure.com:443/workflows/13a1c2517dea4c10b56508b83069102a/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=pK7xXXMjndVz0Kfdp4glDimhKgFscAhZBqlvSGKd9K4","redirectList](https://prod-102.westus.logic.azure.com:443/workflows/13a1c2517dea4c10b56508b83069102a/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=pK7xXXMjndVz0Kfdp4glDimhKgFscAhZBqlvSGKd9K4%22,%22redirectList)":}

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 September 2021 14:38 UTC](https://discourse.nodered.org/t/multipart-data-json-file/48134/7 "2021-09-06T14:38:02Z")

</div>

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