Open
Description
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:
- REST API Gateway CDK
- AND Dart code that invokes REST endpoint
- 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