Skip to content

Amplify Gen2 Flutter REST API #5252

Open
@mateuszboryn

Description

@mateuszboryn

Description

I can't see an example of Amplify Gen2 Flutter REST API.
There is one for React (https://docs.amplify.aws/react/build-a-backend/add-aws-services/rest-api/set-up-rest-api/), however whatever I try for Flutter, it doesn't work.
Could you please provide some working example of:

  1. REST API Gateway CDK
  2. AND Dart code that invokes REST endpoint
  3. AND authorization using Cognito User pool for that endpoint

Categories

  • Analytics
  • API (REST)
  • API (GraphQL)
  • Auth
  • Authenticator
  • DataStore
  • Notifications (Push)
  • Storage

Steps to Reproduce

There's documentation for React (https://docs.amplify.aws/react/build-a-backend/add-aws-services/rest-api/set-up-rest-api/), but I can't find one for flutter.
I tried to adapt existing example from React to Flutter, however I can't get requests authorized.

// backend.ts
// all the same as https://docs.amplify.aws/react/build-a-backend/add-aws-services/rest-api/set-up-rest-api/
// except for outputs, which is here, adjusted to what Flutter's Gen2 configure() expects:

// add outputs to the configuration file
backend.addOutput({
    custom: {
        rest_api: {
            [myRestApi.restApiName]: {
                aws_region: Stack.of(myRestApi).region,
                url: myRestApi.url,
                authorization_type: "AMAZON_COGNITO_USER_POOLS",
            },
        },
    }
});
// in _configureAmplify()
Map decodedAmplifyConfig = json.decode(amplifyConfig);
var newConfig = {};
newConfig.addAll(decodedAmplifyConfig);
newConfig.addAll(decodedAmplifyConfig["custom"]);
await Amplify.configure(json.encode(newConfig));
// call the API
try {
      final restOperation = Amplify.API.post(
        'cognito-auth-path',
        body: HttpPayload.json({'name': 'Mow the lawn'}),
      );
      final response = await restOperation.response;
      print('POST call succeeded');
      print(response.decodeBody());
    } on ApiException catch (e) {
      print('POST call failed: $e');
    }

The above gives 401

{"message":"Unauthorized"}

Screenshots

No response

Platforms

  • iOS
  • Android
  • Web
  • macOS
  • Windows
  • Linux

Flutter Version

3.22.3

Amplify Flutter Version

2.3.0

Deployment Method

AWS CDK

Schema

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    REST APIIssues related to the API (REST) Categoryfeature-parityA request for a feature that is fully or partially available on another platform (JS, iOS, Android)feature-requestA request for a new feature or an enhancement to an existing API or category.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions