0

I want to upload a .csv file to my REST API server. The client has the following function:

postFile() {
    var formData = new FormData();
    formData.append("file", this.props.submittedFile)

    axios.post("http://localhost:1337/classify-csv", formData, {
        headers: {
            "Content-Type": "multipart/form-data",
            Authorization: "Bearer " + localStorage.getItem("token"),
            Email: localStorage.getItem("email")
        }
    }).then((response) => {
            ;
        }, (error) => {
            this.unmount();
        });
}

(no worries, I will change from localStorage to cookies soon for storing the token) Now when receiving the data I don't know how to get the file information out of it and how to download it. This is what I got so far on the API side:

router.post("/classify-csv", checkAuthentication, async (req: Request, res: Response) => {
    logging.info("Classification post", "Classification post-request called.");
  
    try {
        const classificationData = req;
        
        // it ain't much
    }
    catch (err) {
        console.log(err);
    }
});

How can I unpack the file from (in my case) classificationData?

I searched the internet but somehow didn't find a (good) answer.

Theodor Peifer
  • 3,097
  • 4
  • 17
  • 30
  • 1
    Don't set the `Content-type` request header, Axios will do that for you and will include the required mime boundary tokens automatically – Phil May 27 '21 at 00:04
  • 1
    Assuming your API is an Express app, the most popular choice is [multer](https://github.com/expressjs/multer) – Phil May 27 '21 at 00:05

0 Answers0