Responses with no body

1,216 views
Skip to first unread message

bloudermilk

unread,
Jul 27, 2012, 12:43:29 PM7/27/12
to ruby-...@googlegroups.com
This started off as a question on Github (sorry about that) where @dblock pointed out that an empty response body is not valid JSON. This leads me to a more design-related question that is: Should my API be returning empty response bodies? If 95% of my endpoints return JSON bodies but a couple endpoints (for example, DELETE requests) return no body and the content type text/plain, is this bad design?

Any input is appreciated.

Robert Ross

unread,
Jul 27, 2012, 1:01:36 PM7/27/12
to ruby-...@googlegroups.com
The standard for a delete request is to return a 200 OK with an empty response body if I remember correctly.

As for the content type, not sure on that one. I would imagine keeping it application/json would be the most consistent approach.

Sent from my iPhone

Daniel Doubrovkine

unread,
Jul 28, 2012, 5:12:55 PM7/28/12
to ruby-...@googlegroups.com
In my experience with the real world I often found that I wanted the DELETE to return the destroyed object. It made code that provides messages like "XYZ has been deleted" much simpler - no need to keep some model around. 

Other than that, there's a fundamental question here. Is an empty string valid JSON? If we think it is, we should probably handle "" and nil in Grape differently than we do now.

Robert Ross

unread,
Aug 21, 2012, 7:26:47 PM8/21/12
to ruby-...@googlegroups.com
I think it is. Why not return {}  ?

Daniel Doubrovkine

unread,
Aug 21, 2012, 7:30:25 PM8/21/12
to ruby-...@googlegroups.com
True. That's an empty JSON, but not an empty body though. An empty body would have a content length of zero.

A possible implementation could be something like `empty!` or `terminate!` (can't think of a good name) that would raise an exception that, once caught, produces a body with content-length 0.

Andrew Cove

unread,
Jul 13, 2013, 12:50:45 AM7/13/13
to ruby-...@googlegroups.com
Did anything ever come of this?

I'm tracking down a single endpoint that is turning a 'text/plain' nil response into "null" as the body. Is there a better approach?

Rob Hunter

unread,
Jul 13, 2013, 3:28:21 AM7/13/13
to ruby-...@googlegroups.com

I came across a similar need a while back.

If I remember the HTTP RFC correctly, it's appropriate for a response without a body to have no Content-Type header at all.

There are a few situations that need an empty response. Definitely HEAD requests, but also
204 No Content and some others.

I've got some time this weekend, so I'll look at putting together a pull request.

-Hunter

--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages