Why can't I receive plain text sent in a POST request body?
The request made from a client browser:
var xhr = new XMLHttpRequest();
xhr.open("POST", "/MyRoute/MySubRoute");
xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
xhr.send("hello!");
Using Express with my node server:
app.post('/MyRoute/MySubRoute', function(req, res) {
console.log("Received:"+require('util').inspect(req.body,{depth:null});
res.send();
});
Logged to the console I get:
Received:{}
I've tried with text/plain
(no charset), with the same result. If I change my content type to application/json
and pass a simple JSON string it works fine.
Summarising the above comments which answer the question:
Add
app.use(express.text())
You can read more about it here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With