aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.31k stars 242 forks source link

Unable to perform multipart request #525

Open fotiDim opened 3 years ago

fotiDim commented 3 years ago

Describe the bug We are using the REST API package and we are trying to perform a multipart request to upload a jpg image. The is no documentation about how to perform such a request and all our attempts have failed. The equivalent request using the http package is:

var request = http.MultipartRequest('POST', Uri.parse(Config.endpoint + path));
request.headers['Content-Type'] = 'multipart/form-data';
request.files.add(
  http.MultipartFile.fromBytes('image', image, filename: 'image.jpg'),
);
http.StreamedResponse response = await request.send();

Are multipart request supported? If yes it would be nice to have an example.

fjnoyp commented 3 years ago

Hi @fotiDim

What attempts have you made and what was the resulting behavior / error message?

Have you tried enabling binary support to allow JPEG file types to be received by the API Gateway? https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-content-encodings-examples-image-s3.html#api-gateway-content-encodings-example-upload-image-to-s3

Furthermore, have you considered using our Ampliy Flutter Storage plugin that allows for quick and easy upload and retrieval of images to S3? https://docs.amplify.aws/lib/storage/getting-started/q/platform/flutter

fotiDim commented 3 years ago

Hi @fotiDim

What attempts have you made and what was the resulting behavior / error message?

I do not remember the exact things we tried as long time has passed. I remember there was no obvious way to set a binary object or a multipart request. We have a working example using the http package which is the code that I posted above. We are basically looking for the equivalent code. Have you tried enabling binary support to allow JPEG file types to be received by the API Gateway? https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-content-encodings-examples-image-s3.html#api-gateway-content-encodings-example-upload-image-to-s3

Since the code that uses the http package works I don’t think it is an issue with the gateway.

Furthermore, have you considered using our Ampliy Flutter Storage plugin that allows for quick and easy upload and retrieval of images to S3? https://docs.amplify.aws/lib/storage/getting-started/q/platform/flutter

We are aware of this option. Can’t say if it was taken into account when the service was implemented. This is a preexisting API that we are consuming and changes although theoretically possible are not easy.

luiscib3r commented 7 months ago

You can do something like this.

Install http package and then:

import 'package:amplify_core/amplify_core.dart';
import 'package:http/http.dart' as http;

Future<void> pdf2Text(Uint8List fileBytes) async {
    const path = 'pdf2text';
    final uri = Uri.parse('http://localhost'); // stub uri to create a request

    final request = http.MultipartRequest(
      'POST',
      uri,
    );

    request.files.add(
      http.MultipartFile.fromBytes(
        'file_bytes',
        fileBytes,
        filename: 'file.pdf',
      ),
    );

    final operation = Amplify.API.post(
      path,
      body: HttpPayload.streaming(request.finalize()),
      apiName: 'PDF2Text', // Your api name, get from amplifyconfiguration.dart
      headers: request
          .headers, // Use request headers to get the Content-Type with form data boundary
    );

    final response = await operation.response;

    print(response.decodeBody());
  }