0

I implemented a small REST API with expressjs and once I provided it to my users (internal admin team) they asked to have an additional newline at the end of each response.

In code:

var app = require('express')();
app.get('/thing/id1', function (req, res) {
   res.send('data1');
})

vs:

app.get('/thing/id2', function (req, res) {
   res.send("data2\n");
})

The background is that in case they use the API via curl

$ > curl -s http://localhost:3000/thing/id1
data1$ >

They want to avoid the extra echo to have a nice output

$ > curl -s http://localhost:3000/thing/id1 ; echo
data1
$ >

I hate the additional newline in the code and it feels wrong to send it along, but I get the usecase.

So is there any argument, best practice or standard to follow in that regard?

Cheers.

pagid
  • 13,559
  • 11
  • 78
  • 104
  • 2
    "Nice output" is the responsibility of the presentation layer, not the API. – Paul Abbott Jul 07 '15 at 16:18
  • 1
    A proper shell (like `zsh` with the `PROMPT_CR` and `PROMPT_EOL_MARK` options) can be configured to handle this without the need to change your server. – robertklep Jul 07 '15 at 16:48

1 Answers1

3

I agree with @PaulAbbott – your API should serve data, not presentation.

But you could also try res.send("data2\n\r");

Some prompts recognize \n as a newline, and others \r. https://stackoverflow.com/a/1761086/922593

Community
  • 1
  • 1
matthoiland
  • 912
  • 11
  • 24