0

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.

Doc
  • 10,831
  • 3
  • 39
  • 63
Eesa Munir
  • 83
  • 9
  • 1
    Possible duplicate of [How to upload image in Flutter?](https://stackoverflow.com/questions/44841729/how-to-upload-image-in-flutter) – Richard Heap May 02 '19 at 17:08
  • This answer may help -https://stackoverflow.com/questions/4083702/posting-a-file-and-associated-data-to-a-restful-webservice-preferably-as-json. Specifically the base64 string part of the answer Then just use the dart:convert lib to json.encode the map. If I have understood your question correctly? – user8467470 May 02 '19 at 21:32

1 Answers1

1

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});
M4X_
  • 497
  • 1
  • 5
  • 12