I am using an express router to receive http post request at location + "/info".
The code for that looks like this
let http = new XmlHttpRequest();
http.open("POST",location + "/info");
http.send(JSON.stringify(obj));
this is what obj looks like
class Obj{
size = "";
length = "";
constructor(size, length)
{
this.size = size;
this.length = length;
}
}
let obj = new Obj("test1","test2");
The code I am using to set up the router is this
router.post("/info", (req,res)=>{
console.log(req.body);
res.send(req.body);
})
When I logged the body of the request, it returned a blank array. How do I fix this?
>Solution :
The problem you are facing results in you not having a header. Without the header, the server won’t know how to process the information given in the body.
To create the header, you can use the following code bellow
http.setRequestHeader("Content-type", "application/json");
This will tell the server to process the information as json, and the console will log the object.