node-restify
node-restify copied to clipboard
res.send can't send 0, false and null
Sometimes it makes sence to send data in simple raw form like 0 or false, but Response.send ignores any falsy body.
It sends them just fine - you just need to make sure you are sending a valid error code and response body.
EX:
var restify = require('restify'),
app = restify.createServer();
app.get('/', function(req, res) {
res.send(200, 'false');
});
The first argument you pass to restify is used as the status code, the second is the content you are attempting to send. Remember, the content moving about always ends up represented as strings. There is no such thing as a boolean "false", or a "null" value. However, there is a word 'false' and a word 'null' that can be used instead.
But false, null, and numbers are valid JSON values (there is such things), so why one can not return them just as is. JSON strings 'false' and '"false"' are not the same. 'false' represents false value, '"false"' represents a string. Replace 'false' in your code with true, and you'll get true in response, not "true". With false it does not work (as with 0 and null).
@yuriykashin - I agree
app.get('/', function(req, res) {
res.send(200, false);
});
this really gives empty body on the client which is strange please consider changing this
This seems to work fine now.
This issue is a thing again, false
and ''
get casted to null
, while 0
results in an empty body. I have a rest endpoint that should just return a boolean value – I ended up using response.sendRaw('false', { 'Content-Type': 'application/json' });
instead. But 0
, ''
and false
shouldn't be casted in such an inexpected way.
Would anyone mind reopening this issue again?
That 0 results in an empty body actually makes kind of sense if 0 is interpeted as a status code. But the other ones being converted to null
don't.