0

I am trying to send a multipart form in Flutter. I've looked at sample code, and I see all the fields in the network trace except the multipart file. Here is my code:

  final request = http.MultipartRequest('POST', Uri.parse(Env.flowServer));

  // add the text form fields
  final headers = request.headers;
  headers['Cookie'] = Login.cookieHeader;
  final formFields = request.fields;
  formFields['jobId'] = chunk.jobId.toString();
  formFields['flowChunkNumber'] = chunk.flowChunkNumber.toString();
  formFields['flowChunkSize'] = chunk.flowChunkSize.toString();
  formFields['flowCurrentChunkSize'] = chunk.flowCurrentChunkSize.toString();
  formFields['flowTotalSize'] = chunk.flowTotalSize.toString();
  formFields['flowIdentifier'] = chunk.flowIdentifier;
  formFields['flowFilename'] = chunk.flowFilename;
  formFields['flowRelativePath'] = chunk.flowRelativePath;
  formFields['flowTotalChunks'] = chunk.flowTotalChunks.toString();
  formFields['notesByFileName'] = chunk.notesByFileName;

  // add the data
  final formFiles = request.files;
  formFiles.add(http.MultipartFile.fromBytes('file', chunk.file));
  final response = await request.send();

I get a status code of 400 - bad request. I run the flutter app as a Chrome web application so I can do a network trace. I see the data get posted, but it is missing the "file" field.

Here is what the network trace shows me:

POST /handlers/images/flow.php HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Content-Length: 62350
Host: staging.ocgmegamax.com
Origin: http://localhost:64983
Referer: http://localhost:64983/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36
content-type: multipart/form-data; boundary=dart-http-boundary-sFZv4IvT.au3x5Ut1IZI11mjRFwPPiHi4CNiJQLDEj6Wjgaz0Op

the form fields:
jobId: 148951
flowChunkNumber: 1
flowChunkSize: 65536
flowCurrentChunkSize: 60588
flowTotalSize: 60588
flowIdentifier: 60588-sample_upload_photo_smalljpeg
flowFilename: sample_upload_photo_small.jpeg
flowRelativePath: sample_upload_photo_small.jpeg
flowTotalChunks: 1
notesByFileName: {"60588-sample_upload_photo_smalljpeg":"This is a low-res photo of a flower"}

The data is correct for the service I'm calling. When I run a real web app that calls this service successfully, it passes similar data, but I also see the file field. Here is an example:

jobId: 150190
flowChunkNumber: 1
flowChunkSize: 262144
flowCurrentChunkSize: 262144
flowTotalSize: 1074223
flowIdentifier: 1074223-mansion_housejpeg
flowFilename: mansion_house.jpeg
flowRelativePath: mansion_house.jpeg
flowTotalChunks: 4
file: (binary)
notesByFileName: {"1074223-mansion_housejpeg":"test"}

The data I'm passing into the MultipartFile is a List of around 60,000 bytes.

Any help would be appreciated.

shawnlg
  • 351
  • 4
  • 12

1 Answers1

0

Once I added mediaType and fileName it worked. I'm not sure why the browser wasn't showing the field in network trace, but when I had it show the unparsed data, it was there all along.

shawnlg
  • 351
  • 4
  • 12