I can not get image upload to my restAPI server via http package. I spend whole day looking for an answer and didn't find any. My backend require an actually image but I don't seems to manage it with http package.
Future<void> createProfile(Profile profile) async { try { var request = new http.MultipartRequest("POST", Uri.parse(APIPath.createProfile())); request.fields.addAll(profile.toMap()); request.files.add(await http.MultipartFile.fromPath( 'image', profile.image.path, contentType: MediaType('image', '*'))); request.headers['authorization'] = "Bearer $_token"; final response = await request.send(); if (response.statusCode == 200) print('Uploaded!'); notifyListeners(); } catch (error) { throw error; } } My backend is written in node.js and was tested in postman so backend should be fine. However when I tried to upload an image from my front end it gives me error only images allowed which means the image doesn't get to the server(allowed image type is correct). Please any help appreciated
error triggered in Node.js
const fileFilter = (req, file, cb) => { if (file.mimetype === 'image/png' || file.mimetype === 'image/gif' || file.mimetype === 'image/jpg' || file.mimetype === 'image/jpeg') { cb(null, true); } else { const error = new Error('Only images are allowed') error.statusCode = 406; cb(error); } }
request.files.add(await http.MultipartFile.fromPath( 'image', profile.image.path, ));file.mimetypeinnode.js?image/*MediaType('image', '*')toMediaType('image', 'jpg')and it works. Reason why I used'*'was to allowed all image types from my client side to be uploaded to my backend.. So how do you allow all image types to be uploaded without specifying type then?