TL;DR

Set status to 204 AND pass an empty text/data too.

Every now and then I’m coding a web API and I need to send back an empty response, which is HTTP status code 204 (also known as No Content).

This is how to do it properly in Mojolicious, the real-time web framework for Perl:

$c->render(status => 204, data => '');

It has tripped me so many times:

  • about 90% of them I use(d) code instead of status;
  • I keep (hopefully kept) to leave out the data part because it just seems right (if status is 204, why bother?).

I hope writing this will help me avoid the bug the next time!

Update: this suggestion from preaction in the comments below is shorter and maybe easier to remember:

$c->rendered(204);

I say maybe because I pretty much never use rendered, so it’s likely I’ll forget it πŸ™„