multipart/form data nodejs Apollo-graphql

Viewed 534

In my project I am trying to upload a pdf file to a specific API using Apollo-REST-datasources. The Api in question has the following curl command to upload the file:

curl https://api.lexoffice.io/v1/vouchers/123456/files
-X POST
-H "Authorization: Bearer {accessToken}"
-H "Content-Type: multipart/form-data"
-H "Accept: application/json"
-F "file=@{PathToImage}"

So far I've searched the vastly the internet for an answer to this, but this is my request which I believe it is as the docs say to do:


class LexOfficeApi2 extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = "https://api.lexoffice.io/v1/"
  }
  willSendRequest(request) {
    request.headers.append("Content-Type", 'multipart/form-data');
    request.headers.append("Accept", "application/json");
    request.headers.append("Authorization", `Bearer ${LEXOFFICE}`);

  }
  async uploadInvoice() {
    const formData = new FormData()
    formData.append("file", fs.createReadStream('document.pdf'), 'document.pdf');
    return await this.post(`/vouchers/123456/files`, formData)
  }
} 

where I try to upload the "document.pdf" that is located on the server main root folder. However every time I send the request, it fails, stating that the "file was empty".

What am I doing wrong?

0 Answers
Related