My backend server has the following code using Express:
router.post('/test', function(req, res) {
res.end(res.writeHead(200, "custom message");
});
When I make this post request in Node.js using Request library how do I get the "custom message"?
var request = require("request");
request.post(options, function(error, response, body) {
console.log(response.statusCode); //works
console.log(JSON.stringify(response.headers)); //doesn't have my custom message
});
In c# I was able to get this message in the statusDecription property, how does it work in Request library?
Edit: Solved with the following code:
router.post('/test', function(req, res) {
res.end(res.writeHead(200, {"test": "custom message"});
});
then
var request = require("request");
request.post(options, function(error, response, body) {
console.log(response.headers["test"]);
});
From the
http.ServerResponse
API:The
response
argument in therequest
callback is an instance ofhttp.ServerResponse
. What you're looking for is in theresponse.statusMessage
.https://nodejs.org/api/http.html#http_class_http_serverresponse