# Send a file with multipart/form-data to OctoPrint REST API

**URL:** <https://discourse.nodered.org/t/send-a-file-with-multipart-form-data-to-octoprint-rest-api/7534>\
**Category:** General\
**Created:** [3 February 2019 18:19 UTC](https://discourse.nodered.org/t/send-a-file-with-multipart-form-data-to-octoprint-rest-api/7534 "2019-02-03T18:19:15Z")\
**Posts on this page:** 1\
**Showing post:** 30

<div class="post-metadata">

**Author:** ![jmk-randers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jmk-randers/32/49562_2.png) [@jmk-randers](https://discourse.nodered.org/u/jmk-randers)\
**Post date:** [19 October 2021 07:02 UTC](https://discourse.nodered.org/t/send-a-file-with-multipart-form-data-to-octoprint-rest-api/7534/30 "2021-10-19T07:02:50Z")

</div>

@knolleary: thanks for confirming it, I have tried POSTing a multipart with some JSON metadata first and then a filebuffer using the structure described

```auto
{
    "KEY": {
        "value": FILE_CONTENTS,
        "options": {
            "filename": "FILENAME"
        }
    }
}

```

as described.

My msg.headers are set to:

```auto
{
   "Content-Type":"multipart/form-data",
   "Accept":"application/json",
   "Authorization":"Bearer "
}

```

My payload are structured like this (values mocked up for data-protection):

```auto
{
    "CaseID":1234567890,
    "DocumentName":"Testing .pdf POST",
    "Casepart":{
                         "PartId":12345,
                         "PartType":"Person"
                       },
     "Documenttype":
                   {
                        "Id":1,
                        "Name":"TestName"
                    },
   "Beskrivelse":"Test",
   "file":{
               "value": [37,_values left out for readability_,80],
               "options": {
                                  "filename":
                                  "test.pdf"}
                                }
}

```

The reply i get from the endpoint though is ""Object reference not set to an instance of an object." which indicates that one of my values is empty or malformed or maybe that the endpoint cannot recieve the data in this format..

Heres my flow with sensistive data removed:

```auto
[{"id":"e39491ff.49538","type":"pdfmake","z":"f08e748.bb67888","name":"","outputType":"Buffer","inputProperty":"payload","options":"{}","outputProperty":"filebuffer","x":280,"y":60,"wires":[["f20736f5.799d08","88e8a48f.723278"]]},{"id":"d8ae17d6.85ee48","type":"inject","z":"f08e748.bb67888","name":"Test Data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"content\":[\"First paragraph\",\"Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines\"]}","payloadType":"json","x":100,"y":60,"wires":[["e39491ff.49538"]]},{"id":"f20736f5.799d08","type":"debug","z":"f08e748.bb67888","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"filebuffer","targetType":"msg","statusVal":"","statusType":"auto","x":360,"y":120,"wires":[]},{"id":"88e8a48f.723278","type":"change","z":"f08e748.bb67888","name":"request parametre","rules":[{"t":"set","p":"headers","pt":"msg","to":"{\"Content-Type\":\"multipart/form-data\",\"Accept\":\"application/json\",\"Authorization\":\" \"}","tot":"json"},{"t":"set","p":"access_method","pt":"msg","to":"Bearer","tot":"str"},{"t":"set","p":"headers.Authorization","pt":"msg","to":"access_method & \" \" & $flowContext('access_token')","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"{\"CaseID\":1234567890,\"DocumentName\":\"Testing .pdf POST\",\"Casepart\":{\"PartId\":12345,\"PartType\":\"Person\"},\"Documenttype\":{\"Id\":1,\"Name\":\"TestName\"},\"Beskrivelse\":\"Test\",\"file\":{\"value\":\"\",\"options\":{\"filename\":\"\"}}}","tot":"json"},{"t":"set","p":"url","pt":"msg","to":"endpointurl","tot":"env"},{"t":"set","p":"method","pt":"msg","to":"POST","tot":"str"},{"t":"set","p":"payload.file.options.filename","pt":"msg","to":"test.pdf","tot":"str"},{"t":"set","p":"payload.file.value","pt":"msg","to":"filebuffer","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":60,"wires":[["83fc6a59.f49988","9895b6e0.172548","d8424031.e4c7"]]},{"id":"d8424031.e4c7","type":"http request","z":"f08e748.bb67888","name":"","method":"use","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":710,"y":60,"wires":[["c58d18bc.854a28"]]},{"id":"c58d18bc.854a28","type":"debug","z":"f08e748.bb67888","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":60,"wires":[]},{"id":"83fc6a59.f49988","type":"debug","z":"f08e748.bb67888","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"headers","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":160,"wires":[]},{"id":"9895b6e0.172548","type":"debug","z":"f08e748.bb67888","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":200,"wires":[]}]

```

Any inputs?

I have read the following post to try and get a bit wiser.. but to no avail (so far)

> [@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/2):
>
> Hi @vmoscolini At a glance, I would expect the following to work: Assuming msg.payload.byteArray is a Buffer containing the pdf and msg.payload.fname is a String with the filename in. I also assume you have configured the HTTP Request node to do a POST rather than the default GET? It may be worth passing the message to a Debug node so you have verify exactly what you're passing to the Request node.

[https://flows.nodered.org/flow/cbf44e064b9406a1175a7e8d589f66ac](https://flows.nodered.org/flow/cbf44e064b9406a1175a7e8d589f66ac)

> [@Multipart data (json +file)](https://discourse.nodered.org/t/multipart-data-json-file/48134):
>
> 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?

> [@Posting form data with function](https://discourse.nodered.org/t/posting-form-data-with-function/20790/6):
>
> Heartfelt thanks for providing this solution here heart_eyes

---

_[View the full topic](https://discourse.nodered.org/t/send-a-file-with-multipart-form-data-to-octoprint-rest-api/7534)._
