Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
How to make a POST request from Flutter to Amplify REST API

Following the getting started documentation for Amplify Flutter REST API, the auto-generated POST request below for ExpressJS results in the following error:

SyntaxError: Unexpected token ' in JSON at position 1 at JSON.parse

The GET request returns fine, but when changing to POST and adding the body field below from the documentation the error is returned. How can a POST request be successfully called from Flutter using the Amplify REST API?

Flutter GET Request - (no error)

Future<void> callAPI() async {
    try {
      RestOptions options = RestOptions(
          path: '/example',
          apiName: 'ExpressJSRESTAPI'
      );
      RestOperation restOperation = Amplify.API.get(
          restOptions: options
      );
      RestResponse response = await restOperation.response;
      print('GET call succeeded');
      print(new String.fromCharCodes(response.data));
    } on ApiException catch (e) {
      print('GET call failed: $e');
    }
  }

Flutter POST Request (throws error)

Future<void> callAPI() async {
    try {
      RestOptions options = RestOptions(
          path: '/example',
          body: Uint8List.fromList('{\'name\':\'Mow the lawn\'}'.codeUnits),
          apiName: 'ExpressJSRESTAPI'
      );
      RestOperation restOperation = Amplify.API.post(
          restOptions: options
      );
      RestResponse response = await restOperation.response;
      print('POST call succeeded');
      print(new String.fromCharCodes(response.data));
    } on ApiException catch (e) {
      print('POST call failed: $e');
    }
  }

ExpressJS GET request path

app.get('/example', function(req, res) {
  // Add your code here
  res.json({success: 'get call succeed!', url: req.url});
});

ExpressJS POST request path

app.post('/example', function(req, res) {
  // Add your code here
  res.json({success: 'post call succeed!', url: req.url, body: req.body})
});
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Based on the github discussion here the issue happening due to unneeded escape character.

Change your body parameter to this:

body: Uint8List.fromList('{"name":"Mow the lawn now!"}'.codeUnits)

or

body: ascii.encode('{"name":"Mow the lawn"}')
about 4 years ago · Santiago Trujillo Report

0

From the documentation, notice the reverse slash (\) in your RestOptions' fromList arguments.

RestOptions options = RestOptions(
    path: '/todo',
    body: Uint8List.fromList('{\'name\':\'Mow the lawn\'}'.codeUnits)
);
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!