"head" renders valid JSON if response type is "application/json" #8124

Closed
wants to merge 1 commit into
from

Projects

None yet

4 participants

@adimichele
Contributor

I was getting javascript exceptions from jQuery using $.post(...) to an action that rendered using head :ok because head returns a blank response body (which is not valid JSON) even though $.post requested "application/json" and the response type was "application/json".

I went ahead and modified "head" put valid JSON in the response body if the response format is "application/json".

This may turn into a philosophical argument, but I would say that if the response claims that its content type is "application/json" that its body be valid for that type.

Owner

I think this has been discussed in the past but I can't remember conclusions

/cc @spastorino @josevalim

Member

head :ok is shortcut for a 200 with no body. You want render :text => "{}", :status => 200.

I'm 👎 on this.

This may turn into a philosophical argument, but I would say that if the response claims that its content type is "application/json" that its body be valid for that type.

This is true, but just because the client asks for application/json doesn't mean it has to get application/json. That said, we shouldn't return invalid JSON while serving up something that claims to be.

Contributor

Saw it was discussed briefly here: #2315, but it doesn't look like anything was really decided.

@steveklabnik Yeah I was wondering where people would land using render vs head... render :text => "{}", :status => 200 is actually exactly how I fixed it in my code. I just thought it would be nice if head :ok was more magical and that the response body matched the content-type, as you mentioned. I figured a little text in the response body wouldn't hurt anybody.

Member

The more that I think about it, since the Content-Length would be 0, I don't think that it matters that "" is not valid JSON.

Quoting from: http://tools.ietf.org/html/rfc2616#page-54

The HEAD method is identical to GET except that the server MUST NOT
return a message-body in the response.

I don't see anything unclear there, that could lead to a philosophical debate. "MUST NOT" MUST (pun not intended) be interpreted as per: http://tools.ietf.org/html/rfc2119 which means:

MUST NOT - This phrase, or the phrase "SHALL NOT", mean that the
definition is an absolute prohibition of the specification.

If someone expects a body, they should not use a HTTP verb that prevents returning a body in the first place.

Member

Yep, there we go. Thanks for fixing my laziness.

Contributor

Guys, HEAD is an HTTP method... we're talking about he Rails head function that generates the response to what could be any HTTP verb (GET, POST, etc.). We could call that function bananas and it wouldn't change those semantics.

Nevertheless I think you're basically right... head must have been designed to respond to the HEAD verb and any other use is done at your own peril.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment