Nestjs call rest attachments api not working

Im using nestjs to create an API that receives a file and then I want to send that file as attachment to Jira API but I can’t make it work

async addAttachment(
    @Param('id') id: string,
    @UploadedFile() file: Express.Multer.File
  ) {
  // logging the files gives:

 {
  fieldname: 'file',
  originalname: 'cockpit-print.png',
  encoding: '7bit',
  mimetype: 'image/png',
  buffer: <Buffer 61 05 00 ... 93621 more bytes>,
  size: 93671
}

then in order to call jira api I do:

    const attachmentHeaders = {
      headers: {
        Authorization: `Basic ${this.credentials}`,
        Accept: 'application/json',
        'X-Atlassian-Token': 'no-check',
      },
    };

    const form = new FormData();
    const stream = Readable.from(file.buffer.toString());
    form.append('file', stream);
    return axios.post(
      `${this.baseUrl}issue/${id}/attachments`,
      form,
      attachmentHeaders
    );
}

but this gives the error: “Error: Request failed with status code 415”

any ideas? ty

If someone is struggling here is the solution :slight_smile:

    const form = new FormData();
    form.append('file', file.buffer, file.originalname);

    const attachmentHeaders = {
      headers: {
        Authorization: `Basic ${this.credentials}`,
        Accept: 'application/json',
        'X-Atlassian-Token': 'no-check',
        ...form.getHeaders(),
      },
    };
1 Like