Estoy probando realizar un POST con un código en FLUTTER a un servidor local en el puerto 3000. La función que utilizo es:
Future<void> realizarPost() async {
final url = Uri.parse('http://localhost:3000/post');
final datos = {
"titulo": "Mario",
"body": "El propio llamando desde Flutter",
};
final respuesta = await http.post(url, body: (datos));
if (respuesta.statusCode == 200) {
print('Solicitud POST exitosa');
} else {
print('Error al realizar el POST: ${respuesta.statusCode}');
}
}
Pero recibo desde el terminal el error:
[nodemon] 3.1.3 [nodemon] to restart at any time, enter
rs
[nodemon] watching path(s): . [nodemon] watching extensions: js,mjs,cjs,json [nodemon] startingnode app.js
Una API de Node JS se escucha en el puerto: 3000 la base de datos está conectada CREATING POST: { _id: new ObjectId('667ed51765c758a8344323a1') } Error al insertar el documento: Error: Post validation failed: body: Pathbody
is required., title: Pathtitle
is required. at ValidationError.inspect (E:\node-js\api_server\node_modules\mongoose\lib\error\validation.js:50:26) at formatValue (node:internal/util/inspect:805:19) at inspect (node:internal/util/inspect:364:10) at formatWithOptionsInternal (node:internal/util/inspect:2298:40) at formatWithOptions (node:internal/util/inspect:2160:10) at console.value (node:internal/console/constructor:351:14) at console.warn (node:internal/console/constructor:384:61) at E:\node-js\api_server\controllers\posts.js:53:13 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { errors: { body: ValidatorError: Pathbody
is required. at validate (E:\node-js\api_server\node_modules\mongoose\lib\schemaType.js:1385:13) at SchemaType.doValidate (E:\node-js\api_server\node_modules\mongoose\lib\schemaType.js:1369:7) at E:\node-js\api_server\node_modules\mongoose\lib\document.js:3060:18 at process.processTicksAndRejections (node:internal/process/task_queues:77:11) { properties: [Object], kind: 'required', path: 'body', value: undefined, reason: undefined, [Symbol(mongoose#validatorError)]: true }, title: ValidatorError: Pathtitle
is required. at validate (E:\node-js\api_server\node_modules\mongoose\lib\schemaType.js:1385:13) at SchemaType.doValidate (E:\node-js\api_server\node_modules\mongoose\lib\schemaType.js:1369:7) at E:\node-js\api_server\node_modules\mongoose\lib\document.js:3060:18 at process.processTicksAndRejections (node:internal/process/task_queues:77:11) { properties: [Object], kind: 'required', path: 'title', value: undefined, reason: undefined, [Symbol(mongoose#validatorError)]: true } }, _message: 'Post validation failed' }
He probado varios esquemas y siempre recibo este error. ¿Alguien ha recibido este error y como se puede solucionar?