I have a product that contains name, price and image. I am using Multer to upload images to MongoDB. As we know for uploading I use form-data. As a POST method everything works fine, but when it comes to change the image, nothing happens. As a PATCH/PUT method, I can easily change the name or price in postman like this
application/x-www-form-urlencoded
name: "Name was changed"
But I can't do the same with form-data for changing the image. Even in the header I do Content-type: multipart/form-data
application/form-data
name: "Name was changed again",
image: file
I wonder how I can change the values of a product (especially an image) using form-data in postman
Assuming you are running this all locally on port 3000. If you're using express then you have to use routes that do something like this:
//Global setup
multer = require('multer'),
upload = multer();
//Initial product upload
router.post('/product', upload.single('productImage'), function (req, res, next) {
/** business logic to get post the image to the Mongo DB
* file: req.file, - contains the image
* data: req.body - this will contain your name and price information
*/
res.status(200).end()
});
//Update product - use a put because you provide all details about a picture or a patch if you only provide some.
router.put('/product/:id', upload.single('productImage'), function (req, res, next) {
/** business logic to get post the image to the Mongo DB
* file: req.file, - contains the image
* data: req.body - this will contain your req.body.name and req.body.price information
*/
res.status(200).end()
});
Then setup your postman request as such to test your routes respectively:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.