0

What should I specify the media type in the content type if the image can be jpeg or png ?

 var postUri = Uri.parse('$serverUrl/store/signup');
 var request = http.MultipartRequest('POST', postUri)
 ..fields['username'] = 'Username'
 ..files.add(await http.MultipartFile.fromPath(
      'profilePic',
      profilePic!.path,
      contentType: MediaType('image', 'jpeg'),
    ));
  var response = await request.send();
Flutter Dev
  • 488
  • 3
  • 18

1 Answers1

2

You are supposed to specify the correct type. image/png for PNG and image/jpeg for JPEG. The whole point of media types is to tell the other party what is the actual type of the data you are sending.

When receiving data, you are allowed to specify multiple different media types that you will accept, or to specify wildcards (e.g. image/*). But when you are sending data, it cannot be JPEG and PNG at the same time. Therefore, you must state the actual type.

Étienne Miret
  • 6,448
  • 5
  • 24
  • 36
  • ok then how can I know the extension of the image the user chooses from the gallery ? in order to set the exact media type ? – Flutter Dev Jan 09 '23 at 17:09
  • That's worth a separate question :) It depends how you get this image, some API will give it to you, otherwise you'll have to sniff it by inspecting the first few bytes. – Étienne Miret Jan 09 '23 at 17:16
  • @Flutter Dev you can check extension of the image from its file path which contains file path including name & file extension. – Vishal Zaveri Jan 09 '23 at 17:31