Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

113
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda