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

**URL:** <https://discourse.nodered.org/t/form-data-multipart-encoded-request-with-pdf-file-attached-into-the-body/44749>\
**Category:** General\
**Created:** [26 April 2021 13:34 UTC](https://discourse.nodered.org/t/form-data-multipart-encoded-request-with-pdf-file-attached-into-the-body/44749 "2021-04-26T13:34:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmoscolini](https://avatars.discourse-cdn.com/v4/letter/v/d6d6ee/32.png) [@vmoscolini](https://discourse.nodered.org/u/vmoscolini)\
**Post date:** [26 April 2021 13:34 UTC](https://discourse.nodered.org/t/form-data-multipart-encoded-request-with-pdf-file-attached-into-the-body/44749/1 "2021-04-26T13:34:45Z")

</div>

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](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:

```auto
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:

> [@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

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

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

```

We also tried:

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

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

```

And:

```auto
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!

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 April 2021 13:39 UTC](https://discourse.nodered.org/t/form-data-multipart-encoded-request-with-pdf-file-attached-into-the-body/44749/2 "2021-04-26T13:39:33Z")

</div>

Hi @vmoscolini

At a glance, I would expect the following to work:

> [@vmoscolini](#):
>
> ```auto
> msg.headers = {
> "content-type" : 'multipart/form-data'
> };
> 
> msg.payload = {
> "file": {
> "value": msg.payload.byteArray,
> "options": {
> "filename": msg.payload.fname
> }
> }
> }
> 
> ```

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.

---

<div class="post-metadata">

**Author:** ![vmoscolini](https://avatars.discourse-cdn.com/v4/letter/v/d6d6ee/32.png) [@vmoscolini](https://discourse.nodered.org/u/vmoscolini)\
**Post date:** [26 April 2021 14:02 UTC](https://discourse.nodered.org/t/form-data-multipart-encoded-request-with-pdf-file-attached-into-the-body/44749/3 "2021-04-26T14:02:14Z")

</div>

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!

```auto
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!

---

<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:** [10 May 2021 14:02 UTC](https://discourse.nodered.org/t/form-data-multipart-encoded-request-with-pdf-file-attached-into-the-body/44749/4 "2021-05-10T14:02:24Z")

</div>

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