0

backend is taking image as fileHere is my http post method where I had send data like title,link,description as string but when I tried to post image file it didn't work

void create(String title, link,description,File image) async {
try {
  Response response = await post(
      Uri.parse(NetworkConstants.BASE_URL + 'organization/opportunity'),
      headers: {
        "Authorization": "Bearer $token",
        "Accept": "application/json"
      },
      body: {
        'title': title,
        'banner':Image.file(image),
        'link': link,
        'description':description
      });
  if (response.statusCode == 200) {
    var data = jsonDecode(response.body.toString());
    // print(data);
    showToast(context, data['message']);
    Navigator.pop(context);
  } else {
    var data = jsonDecode(response.body.toString());
    showToast(context, data['errors'].toString());
  }
} catch (e) {
  print(e);
  showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("Exception:"),
          content: Text(e.toString()),
          actions: [
            TextButton(
              child: Text("Try Again"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            )
          ],
        );
      });
}

} How to use multipart and send those fromdata also inside body

  • What didn't work? Can you show us the error? – Ozan Taskiran Sep 04 '22 at 13:37
  • 1
    you can send the image to the server as [encoded string in base64](https://stackoverflow.com/questions/50036393/how-to-convert-an-image-to-base64-image-in-flutter) and then decode it in the server side back to its original form – HII Sep 04 '22 at 13:39
  • Does this answer your question? [Use Flutter to send a http Post-Request (containing an image) to a Flask API](https://stackoverflow.com/questions/64196318/use-flutter-to-send-a-http-post-request-containing-an-image-to-a-flask-api) – user18309290 Sep 04 '22 at 16:54

0 Answers0