0

this is the form of the API endpoint enter image description here

when I send a post request it always fails, then I try to only send text without pictures and it works. with data accommodated through the model. i have service api code like this :

Future<RekomModel> createRekom(RekomModel data, int id) async {
    try {
      final token = await AuthService().getToken();

      final res = await http.post(
        Uri.parse('$baseUrl/rekomendasi-penilaian/$id/store'),
        headers: {
          'Authorization': token,
        },
        body: data.toJson(),
      );

      if (res.statusCode == 200) {
        RekomModel uploadData = RekomModel.fromJson(jsonDecode(res.body));
        return uploadData;
      } else {
        throw jsonDecode(res.body)['message'];
      }
    } catch (e) {
      rethrow;
    }
  }

how do i solve this well, what should i change in my code

  • In Postman, does the code work correctly and only not work in Dart? – MendelG Aug 07 '23 at 19:23
  • It needs to be a [multipart post request](https://pub.dev/documentation/http/latest/http/MultipartRequest-class.html). Like this [answer](https://stackoverflow.com/questions/49125191/how-to-upload-images-and-file-to-a-server-in-flutter). If using body json, send the image in base64. – Chance Aug 07 '23 at 19:37
  • You must use ``http.MultipartRequest`` for post images and data. For more you can view this [example](https://medium.com/nerd-for-tech/multipartrequest-in-http-for-sending-images-videos-via-post-request-in-flutter-e689a46471ab) – Pradip D. Aug 08 '23 at 06:29

0 Answers0