How to upload files using HTTP Requests

Introduction

In many cases sending multipart files in POST request is necessary, so in this example we will show you how to do it. We will upload a JSON file to our file repository, but this is the same for any other multipart upload.

Example

 

  1. Creating the file content

    We can create the content of the file during execution, or we might have it already loaded from a previous call. Anyhow, the most important aspect is that the content itself is defined as a binary Buffer. In this example, the content will be a JSON String and we will transform it into a binary buffer.

    For that, in a “function” node, we will create the JSON String and transform it into a binary buffer.

  2. Preparing the request

    We are going to make use of the Platform Management API for file uploads. In our case we will have to define the URL, method and headers:


  3. Preparing the multipart

    In order to upload the file, we will have to set up the Multipart by hand.

    First we set the ContentType definition to multipart in the headers

    Then the multipart body is completed, making sure that it is a binary buffer.


     

  4. Request invocation

    Next, we define the HTTP Request node, so that it takes the method and URL of the message from the previous node:




  5. Result of the execution

    The flow will look like this and we can run it to upload the file.


     

Here is an example for you to import and test:

[{"id":"d8595d192abf4955","type":"tab","label":"Flow 6","disabled":false,"info":"","env":[]},{"id":"1ede2a06b2344b9c","type":"function","z":"d8595d192abf4955","name":"Prepare POST","func":"\nmsg.payload ='[{\"result\":{\"value\":3,\"device\":\"device1\"}},{\"result\":{\"value\":8,\"device\":\"device2\"}}]';\nlet content = Buffer.from(msg.payload, 'binary');\n\nmsg.url = \"http://controlpanelservice:18000/controlpanel/binary-repository?metadata=&repository=MONGO_GRIDFS\";\nmsg.method = \"POST\";\n\nmsg.headers = {\n \"Content-Type\": \"multipart/form-data; boundary=------------------------d74496d66958873e\",\n \"X-OP-APIKey\": \"<insertYourTokenHere>\"\n}\nmsg.filename = \"myFile.json\"\nlet stringHeader = '--------------------------d74496d66958873e\\r\\n' +\n 'Content-Disposition: form-data; name=\"select\"\\r\\n' +\n '\\r\\n' +\n 'true\\r\\n' +\n '--------------------------d74496d66958873e\\r\\n' +\n 'Content-Disposition: form-data; name=\"print\"\\r\\n' +\n '\\r\\n' +\n 'true\\r\\n' +\n '--------------------------d74496d66958873e\\r\\n' +\n 'Content-Disposition: form-data; name=\"file\"; filename=\"' + msg.filename + '\"\\r\\n' +\n 'Content-Type: image/png\\r\\n' +\n '\\r\\n';\nlet stringFooter = '\\r\\n' +\n '--------------------------d74496d66958873e--\\r\\n';\n// Los datos vienen en binario, por lo que hay que ponerlo todo en el mismo formato. \n// Es mejor trabajar con Buffers ya que nodejs usa unicode y utf-16 para los strings.\nlet header = Buffer.from(stringHeader, 'binary');\nlet footer = Buffer.from(stringFooter, 'binary');\n// Concatenamos el header y el footer a los datos que nos llegan.\nlet datos = Buffer.concat([header, content, footer])\nmsg.payload = datos;\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":240,"wires":[["e877399494e0ad0e"]]},{"id":"86461cc276538dd0","type":"inject","z":"d8595d192abf4955","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":240,"wires":[["1ede2a06b2344b9c"]]},{"id":"2cdac17babe5dbea","type":"debug","z":"d8595d192abf4955","name":"Result debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":850,"y":300,"wires":[]},{"id":"e877399494e0ad0e","type":"http request","z":"d8595d192abf4955","name":"UPLOAD FILE","method":"use","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":620,"y":300,"wires":[["2cdac17babe5dbea"]]}]