Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multipart file uploads to AWS S3 fail with cuppertino_http but work with HttpClient: Missing content length #1236

Closed
escamoteur opened this issue Jun 18, 2024 · 5 comments · Fixed by #1247
Assignees
Labels
package:cupertino_http Issues related to package:cupertino_http type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@escamoteur
Copy link

escamoteur commented Jun 18, 2024

While exited about the new cupertino_http package we found out, that we currently can't use it because it does not att a Content-Length header in Multipart file uploads and if I add this header manually I get an 400 from S3 with the message that the request isn't a well-formed multipart request.

See how they compare:

image

I also attach the two requests as raw text files ( I only replace our URLs with XXX)
I also wonder why the Cupertino Client includes data from sentry in a Request that has nothing to do with sentry. this could be a privacy violation.

this is how we do the Upload:

  @override
  Future<MediaUploadPayload> uploadImage(File file) async {
    final api = S3Api(di<ApiClient>());
    final s3Response = await api.getS3PresignedData();
    final S3AccessData accessData = s3Response!.data!;

    final String fileExt = file.path.split('.').last;

    final contentType = lookupMimeType(file.path) ?? 'application/octet-stream';

    var fileLength = await file.length();
    final multipartFile = http.MultipartFile(
      'file',
      file.openRead(),
      fileLength,
      contentType: MediaType.parse(contentType),
    );

    var relativeImagePath = generateImagePath(fileExt);

    final multipartRequest =
        http.MultipartRequest('POST', Uri.parse(accessData.endpointUrl));

    multipartRequest.fields.addAll({
      for (final field in accessData.params.entries)
        field.key: field.value.toString(),
    });
    multipartRequest.fields.addAll({
      'Content-Type': contentType,
      'key': relativeImagePath,
    });
    multipartRequest.files.add(multipartFile);

    final multipartResponse = await multipartRequest.send();
    final response = await http.Response.fromStream(multipartResponse);

    if (response.statusCode != 201) {
      throw Exception('Failed to upload file to S3: ${response.body}');
    }

As we have customers who complain about uploading errors (Connection closed before...) on iOS we would really like to use the new client. If we can change something in the way we use it any pointer would be really helpful.
As it works with the normal Dart HttpClient and in cronnet it seems to be a problem with cupertino_htttp.
cuppertino_client.txt
HttpClient.txt

cc @brianquinlan

@escamoteur escamoteur added package:cupertino_http Issues related to package:cupertino_http type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Jun 18, 2024
@escamoteur escamoteur changed the title Multipart file uploads to AWS S3 fail with cuppertino_http but work with HttpClient: Missing content length and mal-formed Multipart file uploads to AWS S3 fail with cuppertino_http but work with HttpClient: Missing content length Jun 19, 2024
@escamoteur
Copy link
Author

ok, I now found out the malform was the result that I did not use the correct value for Content-Length.
after adding this to my code:

    multipartRequest.headers.addAll({
      'Content-Length': multipartRequest.contentLength.toString(),
    });

it works.
Still, this should probably be done automatically

@escamoteur
Copy link
Author

just tested with S3 works perfectly

@escamoteur
Copy link
Author

will there be a new version pushed in the near future?

@brianquinlan
Copy link
Collaborator

will there be a new version pushed in the near future?

I'll try to push a new version today or tomorrow.

@escamoteur
Copy link
Author

escamoteur commented Jul 11, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package:cupertino_http Issues related to package:cupertino_http type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants