Click here to Skip to main content
15,513,648 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have to integrate a third-party API for uploading the documents but file uploading will be done on our server and from there I want to send the uploaded file to their api(third party). File type is binary.

I have to achieve this by form-data without uploading the file temporarily.



While hitting the third-party api directly on postman after uploading the file, it is working fine. Please tell me the way to directly pass the file on their server so that we don't need to save the file temporary. Is it needed to convert the file in to binary?

Thanks in advance.


What I have tried:

Tried doing it with the above
I was trying this code :


JavaScript
<pre>app.post("/upload", multer().single("file1"), async (req, res) => {
  var form = new FormData();
  
  let file_data = req.file;
//   let base64String = file_data.data.toString('binary');

  form.append("MID", "M001590");
  form.append("Timestamp", "2019-06-24");
  form.append("DocumentType", "Passport");
  form.append("InvestorId", "373916");
  form.append("Checksum", "7677c50e9d7588312c4179acd64c447d6144bb92bc51f8f7a08119d6f79ce278");
  form.append("File", req.file); //This is the file in binary format that we got from req.file

  // console.log(form);

  let investorExist_response = await axios.post(
    <here third-party api will come>,
    form,
    {
      headers: {
        // "Content-Type": "multipart/form-data",
        "Content-Type": "application/x-www-form-urlencoded"
      },
    }
  );
  
  console.log(investorExist_response.data);
  res.send("done");
});

app.listen(3000, () => {
  console.log("Server is running at PORT : 3000");
});
Posted
Comments
Richard Deeming 25-Aug-22 4:23am    
The file is uploaded as a stream of bytes. You don't "convert to binary" - it's already binary.

You've commented out the correct content type header (multipart/form-data) and replaced it with one that doesn't support file uploads (application/x-www-form-urlencoded).

Beyond that, you haven't told us what the actual problem is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900