Im using AWS Amplify to generate Node Express rest API endpoints.
I recently added a new endpoint, but I keep getting undefined in the request body, and I can't figure out where I might have miss-configured my application.
App.js
var express = require("express");
var bodyParser = require("body-parser");
var awsServerlessExpressMiddleware = require("aws-serverless-express/middleware");
// declare a new express app
var app = express();
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.use(awsServerlessExpressMiddleware.eventContext());
// Enable CORS for all methods
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
var aws = require("aws-sdk");
const https = require("https");
app.post("/refresh", function(req, res){
console.log(JSON.stringify(req.body));
// {}
console.log(JSON.parse(req.body));
//Error: unexpected token o in JSON at position 1
});
I'm testing this in the AWS console with this payload
{
"path": "/refresh",
"httpMethod": "POST",
"header": "{\"content-Type\":\"application/json\"}",
"body": "{\"Username\":\"test\"}"
}
Set header as
"Content-Type: application/json"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.