I need some suggestion on how to send a POST
request using the http
module in Flutter with some parameters.
I need to set the username
to a string(in the body of the request), and also need to set a property to a FILE in the body.
I need some suggestion on how to send a POST
request using the http
module in Flutter with some parameters.
I need to set the username
to a string(in the body of the request), and also need to set a property to a FILE in the body.
The easiest way to do requests on Flutter is to use the Dio package
if your json payload is,
{"username":"johndoe", "image":"base64 image data"}
In dio the code looks like
import "dart:io";
import "dart:convert";
import 'package:dio/dio.dart';
// read image bytes from disk as a list
List<int> imageBytes = File("./image.png").readAsBytesSync();
// convert that list to a string & encode the as base64 files
String imageString = base64Encode(imageBytes);
// Send a post request to server
dio.post("/url-to-post-to", data: {"username":"johndoe", "image":imageString});