Reputation: 2121
This may not be a coding question but this should be relevant enough. I'm using Ruby on Rails and using it for APIs. I just set header[:Status] = "200" and no body content.
But recently on heroku I noticed if I try to get access to it, I get on the webpage:
This page contains the following errors:
error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.
The API still works, but I wonder if this is good practice. I mainly return no body content so the API can be faster. But if it needs to render this error page, then I'm not sure what is best.
Upvotes: 0
Views: 144
Reputation: 359906
If you want to return a "success" with no body, there's a status code for that:
10.2.5 204 No Content
The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.
If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.
The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.
Upvotes: 4