Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Http request info shows up as blank on server

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading