Form-data multipart-encoded request with pdf file attached into the body

Hi all,

Introduction
I need to call an API that requires to pass a pdf file into the body as form-data (Multipart-encoded - method POST).

Api Details
url: https://jurisfai-api.ml/submitfile
method: POST
body: form-data (Multipart-encoded) - key: file - value: binary PDF file
authorization: none

Problem
I could not do it work on node-red with the native "http request" node.
It is working normally using Postman as you can see below:

POST /submitfile HTTP/1.1
User-Agent: PostmanRuntime/7.26.10
Accept: */*
Cache-Control: no-cache
Postman-Token: 5bbcd26d-b5b2-428b-9f7d-52f4ea716b78
Host: jurisfai-api.ml
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------982512850620529427601898
Content-Length: 252089
----------------------------982512850620529427601898
Content-Disposition: form-data; name="file"; filename="processo400exemplo.pdf"
<processo400exemplo.pdf>
----------------------------982512850620529427601898--
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 26 Apr 2021 13:31:14 GMT
Content-Type: application/json
Content-Length: 41
Connection: keep-alive
Strict-Transport-Security: max-age=15724800; includeSubdomains
{"id":"679afc5302ea4a17abb6d435780988e7"}

What we have already tried
We have tried with no success the following post here in the forum:

msg.headers = {
    "content-type" : 'multipart/form-data'
    };


msg.payload = {
    "file": {
        "value": msg.payload.byteArray,
        "options": {
            "filename": msg.payload.fname
        }
    }
}

We also tried:

msg.headers = {
    'content-type' : 'multipart/form-data'
}

msg.payload = {
    "file" : msg.payload.byteArray
}

And:

msg.headers = {
    'content-type' : 'multipart/form-data'
}

msg.payload = {
  file: {
    field: 'file',
    data: msg.payload.byteArray,
    type: 'binary',
    name: msg.payload.fname
  },
  formOptions: {
    params: '',
  }
}

Do you have any idea how to achieve it or what we are doing wrong?

Thank you very much!

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.

1 Like

Hi Nick,

You saved me!
After read your answer I've reviewed the buffer I was passing and that was the problem.

The previous api that generates the buffer does not create as "Buffer" type byte array.
After I converted it to Buffer using Buffer.from(byteArray) it worked!

if (msg.payload['DocNo']){
    if (msg.payload.StreamsInfo.length > 0) {
        var stream = msg.payload.StreamsInfo[0]
        var fname = stream.FileName
        //This is the byte array generated by other api. Using this, it did not work.
        var byteArray = stream.StreamData
        //After converts that to Buffer it worked perfectly!
        var buf = Buffer.from(byteArray)
        msg.payload = {
            fname,
            pdfBase64 : buf.toString('base64'),
            byteArray,
            buf
        }
    } 
    else{
        msg.payload = "NoStream"
    }
}

Thank you very much for you help!

Problem Solved!

1 Like

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