Can't upload images from flutter apps to Djnago rest framework

For my college project, I built an app, where I can update any kind of text info to Django rest API, but I can't send or upload any kind of media file from Flutter apps to Django. If anyone collaborated flutter with the Django rest framework please, help me with appropriate resources.

I didn't find any resources to try to upload media files such as photos, pdf, videos, etc.

Sending image to an API with POST request could be different, depends on the api, In this example image is sent with body paramater, as the documentation of this API says:

import 'package:http/http.dart' as http;

class Api {
  final _url = "https://freeimage.host/api/1/upload";
  final _apiKey = "6d207e02198a847aa98d0a2a901485a5";

  Future<void> uploadImage() async {
    try {
      final image = await ImagePicker().pickImage(source: ImageSource.gallery);

      if (image == null) throw "No image was picked";

      String base64Image = base64Encode(await image.readAsBytes());

      Map<String, dynamic> body = {
        'key': _apiKey,
        'action': 'upload',
        'source': base64Image,
        'format': 'json',
      };

      final response = await http.post(Uri.parse(_url), body: body);

      if (response.statusCode == 200) {
        final result = json.decode(response.body);
        print('Result: $result');
      } else {
        print('Error: ${response.statusCode}, ${response.reasonPhrase}');
      }
    } catch (e) {
      rethrow;
    }
  }
}
Back to Top