I'm encountering an issue with transferring data from the client side to the server side.
Within my application, I have a form that needs to be submitted to the server. Since I am utilizing a library to automatically generate my form from a JSON schema file, I must manually submit the form by essentially making a post
request with my object. Everything seems to be in order so far, but when my request reaches the backend, it arrives with an empty body.
Client-side post request:
const test = {
name: "hello"
}
axios.post("http://localhost:8000/my-route/test", test)
.then(res => console.log("res.data", res.data));
Server route definition:
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(cors());
/* ... */
app.post("/my-route/:type", (req, res) => {
if (!req.params.type) {
res.send({
err: "Please provide type"
});
} else {
console.log('req.body', req.body);
res.send({
success: "Generating type " + req.params.type
});
}
});
My backend log displays req.body {}
rather than req.body { name: "hello" }
.
If I opt to JSON.stringify(test)
, then my log changes to
req.body { '{"name":"hello"}': '' }
, which is not what I had intended.