Trying to send an image from Flutter application to python Flask project using HTTP POST request, but the server log does not register any POST requests being made.
Flutter client side code is making a POST request like this
ElevatedButton(
onPressed: () async {
final ImagePicker _picker = ImagePicker();
var image = await ImagePicker().pickImage(source: ImageSource.gallery);
File file = File(image!.path);
var request = http.MultipartRequest('POST', Uri.parse('http://127.0.0.1:5000/'));
request.files.add(await http.MultipartFile.fromPath('imagefile', file.path));
var data = await request.send;
print("request: " + request.toString());
This is printed to console when I run
flutter: request: POST http://127.0.0.1:5000/
But on the server side log Flask is not receiving a POST request. The log looks like this
127.0.0.1 - - [25/Apr/2024 11:51:14] "GET / HTTP/1.1" 405 -
Flask server side code looks like this
@app.route("/", methods=['POST'])
def home():
if 'imagefile' not in request.files:
return 'File Not Found'
try:
imagefile = request.files.get['imagefile']
if imagefile.filename == '':
return 'No File Selected'
# image manipulation code that i don't think is relevent to the issue
return 'Done'
except Exception as e:
return f'Error: {e}'
if __name__ == '__main__':
app.run()
New contributor
chgaobt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.