node-restify icon indicating copy to clipboard operation
node-restify copied to clipboard

res.send can't send 0, false and null

Open yuriykashin opened this issue 9 years ago • 6 comments

Sometimes it makes sence to send data in simple raw form like 0 or false, but Response.send ignores any falsy body.

yuriykashin avatar Sep 30 '14 09:09 yuriykashin

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.

AngeloR avatar Oct 28 '14 04:10 AngeloR

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 avatar Oct 28 '14 15:10 yuriykashin

@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

DarkPark avatar Apr 05 '15 19:04 DarkPark

This seems to work fine now.

DonutEspresso avatar May 21 '15 07:05 DonutEspresso

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?

janopae avatar May 02 '20 23:05 janopae

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.

janopae avatar May 06 '20 15:05 janopae