# Post an parsed Mail-Attachment to API

**URL:** https://discourse.nodered.org/t/post-an-parsed-mail-attachment-to-api/19334
**Category:** General
**Created:** [19 December 2019 09:17 UTC](https://discourse.nodered.org/t/post-an-parsed-mail-attachment-to-api/19334 "2019-12-19T09:17:01Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![xstable](https://avatars.discourse-cdn.com/v4/letter/x/bb73d2/32.png) [@xstable](https://discourse.nodered.org/u/xstable)
#### Post date: [19 December 2019 09:17 UTC](https://discourse.nodered.org/t/post-an-parsed-mail-attachment-to-api/19334/1 "2019-12-19T09:17:01Z")

</div>

Hey,  
I've done my first steps with Node Red and got some experiences with it.  
But now I stuck on the Problem, that the file won't be send to the API.  
It seems the File-Content is missing.

But let's start from the beginning... what I have so far:

First of all, I have a flow, which parse my incomming Mails to find Emails, which are invoices and have an attachment.  
(This part work like a charm).

It looks like this:

 ![screenshot_10_00_47](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d843eb16089a57b43f9c6bba86de9d8f7c39c5e8.jpeg)

So far, so good. There you see the content of the file is an buffer with the Data.

So let's jump to the next flow, who should send this file to a REST-API. Thats what it looks like:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/53a7b11e6d3d074f5117087a5118380fc87d5148.png)

The Function-Node "API-Template" contain the following source-code.  
Because of try & error, I tried two ways to construct the Payload:

FIRST (full written):

```auto
const { payload: {attachment} } = msg;
node.warn(msg)
        
const body = '----AaB03x\r\n'+
            'Content-Disposition: form-data; name="type"\r\n'+
            'voucher\r\n'+
            '----AaB03x\r\n'+
            'Content-Disposition: form-data; name="file"; filename="'+attachment.filename.replace(" ","")+'"\r\n'+
            'Content-Type: '+attachment.contentType+'\r\n'+
            'Content-Transfer-Encoding: binary\r\n'+
            attachment.content+'\r\n'+
            '\r\n'+
            '----AaB03x\r\n';
            
    const headers = {};
    headers["Content-Type"]="multipart/form-data; boundary=--AaB03x";
    headers["Authorization"]="Bearer <my-secret-token-stay-here>";
    headers["Accept"]="application/json";
    headers["Content-Length"]=body.length.toString();

return {payload:body, headers};

```

The Request for the First Try looks like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7b4952d0ea8673cd8b77d5d8008facff6185666c.png)

SECOND (as Object):

```auto
const { payload: {attachment} } = msg;
node.warn(msg)

msg.headers = {
    'Content-Type': 'multipart/form-data',
    'Authorization': 'Bearer <my-secret-token-stay-here>',
    'Content-Transfer-Encoding':'binary',
    'Content-Length': attachment.content.length.toString()
};
msg.payload = {
    'file' : {
        'value': attachment.content.toString('binary'),
        'options': {
            'filename': attachment.filename.replace(" ",""),
        },
    'type':'voucher'
    },
}

return msg

```

The Request for Second-Try looks like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b56b651a3e187e546b78a1b163547795a5faccef.png)

But the API-Response for both requests is: `statusCode: 500`.

API-Documentation notice: _ **If no file is provided** , the file upload endpoint returns HTTP status 500 ._

BTW: The above Requests are piped to `http-request` node which do a `POST` to the API-Endpoint.

So the question at all: What's wrong with the way I send the file-content. Did I miss something?
