As of writing this, I haven't found an answer that answers my question.
I am building a REST API for an ExpressJS backend server, that has multiple endpoints. One of them is called /company/create
, which accepts company_name
and company_logo
. I'm storing all data in a MySQL database with the help of Sequelize.
I've set the data type of company_logo
to Sequelize.BLOB
. I'm willing to change the data type if there's a better approach.
My question is, when doing a POST request to /company/create
, how do I send the image? The data in the POST request looks something like this:
{
"company_name": "Example Company",
"company_logo": ?
}
I want to test the API before I start using it in the frontend, and I don't how to pass the image into this request.
I hope my question is clear, and not difficult to understand. If it helps, this API is going to be use for a Flutter app.
So what I understood is you want to test your API before integrating it with the front end. Please correct me if I'm wrong. To test your API endpoint that accepts an image file, you can use tools like Postman or simple curl will also work.
The POST request sent by Postman or curl will send the file data as binary data, not as a BLOB data type. The server will need to convert the binary data to a BLOB data type and then store it in the database.
In the case of ExpressJS & Sequelize ORM, you can use the req.file.buffer
property to get the binary data of the uploaded file,
and then save it to the database as a BLOB data type. You may want to try multer
npm package for image upload process purposes.
const company_logo = req.file.buffer;
Follow these steps for the postman
POST
method type.http://localhost:3000/company/create
form-data
option for the body.company_name
and its valueExample Company
company_logo
and the value of the image
file.Alternatively, you use curl as follows,
curl -X POST -H "Content-Type: multipart/form-data" \
-F "company_name=Example Company" \
-F "company_logo=@/path/to/image.jpg" \
http://localhost:3000/company/create