3

I have a node endpoint with takes post request for uploading files. I am able to read the data(file) being send from the browser, now I want to forward the data or the whole request to another server hosted somewhere else. I am using node, express and express-fileupload. I am open to get rid on express-fileupload plugin if required. Following is what I have

app.post("/uploadImage", (req, res) => {
  const headers = {
    "Content-Type": "multipart/form-data"
  };
  axios
    .post("http://139.59.80.251:8080/homely/images", req.files.file.data, {
      headers: headers,
    })
    .then((resp) => {
      res.send(resp);
    })
    .catch((e) => {
      res.status(500).send(e);
    });

});

Any help is appreciated.

1
  • Hey, did you find a problem to this solution ? Been stuck on a similar problem Commented Jul 13, 2023 at 16:31

1 Answer 1

1

You must send the buffer data as form-data.

For this, you have to convert the files (buffer data) via the package form-data.

The code should look like this:

const FormData = require("form-data");
app.post("/uploadImage", (req, res) => {
  const headers = {
    "Content-Type": "multipart/form-data"
  };
  const my_file = new FormData();
  my_file.append("file", req.files.file.data, { filename: req.files.file.name });
  axios
    .post("http://139.59.80.251:8080/homely/images", my_file, {
      headers: headers,
    })
    .then((resp) => {
      res.send(resp);
    })
    .catch((e) => {
      res.status(500).send(e);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.