Skip to content
Advertisement

Cannot POST image to my API Route from React.js

I am creating a MERN app in which I have created an api route /api/books where user can POST a book’s details with it’s image, and the data will be stored in MongoDB.
I am using multer and it stores the image after encoding in binary format in the database.

When I test it with postman, I works perfectly fine and the data is added in database and I receive response 200 status.

enter image description here

But I am facing issues in sending the data from react frontend to the api, I have created controlled forms and am storing the data value in states, then I call the function to POST data to api on form submit using axios, but I get error Multipart: Boundary not found in my terminal, I think the file is not sent through axios correctly.

enter image description here

React form page:

JavaScript

React code to POST data:

JavaScript

Api code:

JavaScript

EDIT

As some of you suggested to use FormData object So I changed the POST funtion to this, I still have the same error Multipart: Boundary not found

JavaScript

Advertisement

Answer

You should be using the FormData class to properly handle this, as demonstrated here.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement