http icon indicating copy to clipboard operation
http copied to clipboard

Multipart form-data request is not working properly. Am i missing something here?

Open 1207roy opened this issue 4 years ago • 13 comments

I am trying to send a multipart form-data request in flutter using http package

Code is as below:

//request var request = http.MultipartRequest('POST', Uri.parse(uploadDocumentsUrl));

//add header fields from requestHeader map request.headers.addAll(requestHeader);

//add fields request.fields['documentType'] = 'random types'; request.fields['notes'] = 'xyz; request.fields['date'] = '2020-05-30'; request.fields['documentName'] = 'abc'

//add image files, File imagefile1, File imagefile2 request.files.add(http.MultipartFile.fromPath('file1', imagefile1.path)); request.files.add(http.MultipartFile.fromPath('file2', imagefile2.path));

print('request files: ${request.files}); //it prints an empty list

//final send the request var response = await request.send();

//check result status print('response code: ${response.statusCode}');

//get data String jsonString; await response.stream.transform(utf8.decoder).listen((value) { jsonString = value; });

Postman screenshot:

image

Issue:

  • "request.files" is empty list!!!
  • No image file is received on the server side!!??

Is there any problem with my code?

1207roy avatar May 30 '20 13:05 1207roy

Hi @1207roy, could you share the exact lines, which you’re using.

Note: please use your code statements with "insert code"

Example:

request.fields['documentType'] = 'random types';
request.fields['notes'] = 'xyz';
request.fields['date'] = '2020-05-30';
request.fields['documentName'] = 'abc' 

valehasadli avatar Sep 19 '20 23:09 valehasadli

same with me here. is this solved?

akbarraihanm avatar Jan 25 '21 12:01 akbarraihanm

Any Solution For this

Manjunath-VM avatar Apr 01 '21 12:04 Manjunath-VM

To me, I get some fields empty, I don't have a clue why, if I set the values, some of them are null:

request.fields['firstName'] = map['nombre']; // With data
request.fields['LastName'] = map['apellido']; // map has the value, request.fields is null
request.fields['password'] = map['password']; //With data
request.fields['phone'] = map['telefono'];  // map has the value, request.fields is null

neoacevedo avatar Jun 08 '21 18:06 neoacevedo

for me it only sends the first image

hayelom123 avatar Feb 09 '22 09:02 hayelom123

This is very weird, there are a lot of people facing this issue (including me), and no one from the flutter community is helping this out!

AjinkyaASK avatar Oct 22 '22 07:10 AjinkyaASK

yess, there is a problem in the multipart request.

utkudenis avatar Nov 18 '22 22:11 utkudenis

Same thing here, 4 days trying to figure out what I was doing wrong (it wasn't me). I finally had to use Dio, at least while the HTTP team resolves this bug.

sebasrod avatar Mar 12 '23 00:03 sebasrod

My server receives empty array of inputs (request.fields) ! This is really weird.

ramtinq avatar Jun 14 '23 23:06 ramtinq

I'm having trouble with the "await request.send()" part. Apparently the request doesn't even leave the application. I appreciate any help.

cristianms avatar Nov 24 '23 14:11 cristianms

I am sending one image file (XFile) from memory along with others fields, but it returns a status code of 400. On Postman it's working properly, and I followed the documentation except that the MultipartFile came from a stream of bytes as opposed to a path.

Spatino19 avatar May 17 '24 17:05 Spatino19

Would it be possible for someone to include a minimum reproduction?

brianquinlan avatar Jun 28 '24 22:06 brianquinlan