Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
Node.js - Pass a file to different servers via API requests

GOAL:
I am trying to take a user-submitted file (a .pdf), handle it in our Node.js server, and upload it to a location dictated by our client's API, which is an AWS S3 storage location.

PROBLEM:
Cannot handle the actual file once it is uploaded to known location.

I have attempted to use various commands from the fs, but they don't work correctly.

  1. There's no local or relative storage, which causes an error with readFile and writeFile
  2. Buffer.from tells me it's not a function. EDIT -- This was due to faulty importing. I can know get a buffer of the pdf if I need it.

The storage location requires the information to come as form-data, and when I submit the information in Postman, it works, i.e. when I pull the file from a local address.

The documentation for the API provides a helpful example using Postman fields: as seen here

Here's my code thus far:

const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');

const uploadUrl = working_location_supplied_as_params.url
const uploadFileName = "string_of_pdf_name.pdf"
const uploadEndpoint = "https://workinglocation.com"
// this file location is https://example.com/file.pdf

That's the address I want to snatch the pdf from.


let testPDF = await axios.get(uploadUrl, {responseType: 'stream'});

// I have also tried this one: 
// let secondaryTestPDF = await axios(uploadUrl);

Is this the ideal way to pull the pdf and use it as data?

Then I call on the API to prepare a spot for the upload. This returns all kinds of complicated AWS authentication details:

const uploadPrepConfig = {
method: 'post',
  url: uploadEndpoint,
  headers: {...},
  data: {
    response_content_type: 'application/pdf',
    response_file_name: uploadFileName,
  },
};

const uploadConfig = await axios(uploadPrepConfig);
// returns a bunch from API

const fileDestURL = uploadConfig.data.url
const objToUpload = uploadConfig.data.fields
// objToUpload has all the information the API says I need to upload to AWS S3
// with the exception of the actual file

for (const [key, value] of Object.entries(objToUpload)) {
  uploadData.append(key,value)
}

// so far so good!

Since I am pretty much mapping 1:1 with the example from the documentation and what successfully works in Postman, I think this is all solid.

That brings us here:

uploadData.append('file',"")
// I have absolutely no idea what to use here.

// all will eventually go here:
const getToS3Please = await axios({
  method: 'post',
  url: fileDestURL,
  headers: {
    'Content-Type': 'multipart/form-data',
  },
  data: uploadData,
})

Any help would be great. Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use a popular and nodejs recommended npm package multer

get your file to your node application through multer and again send it throug axios to other servers

And if you are struggling to upload file through axios try this const FormData = require('form-data');

const form = new FormData();
form.append('my_field', 'my value');
form.append('my_buffer', new Buffer(10));
form.append('my_file', fs.createReadStream('/location/img.jpg'));//give the actual path where you temporarily store data in current server before sending to another servers

axios.post('https://example.com', form, { headers: form.getHeaders() })
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda