Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Rails want to return "head :no_content" for JSON PUT requests?

After I run
rails generate scaffold User
The generated controller function in Rails 3.2.11 for updating a user looks like this:

def update
  @user = User.find(params[:id])

  respond_to do |format|
    if @user.update_attributes(params[:user])
      format.html { redirect_to @user, notice: 'User was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

The line I'm curious about is returning head :no_content for a successful JSON update request. I've done some googling, as I was guessing that this is some sort of RESTful property, to not return the updated object, but I couldn't find anything that claimed that was the case.

Why is this the default, versus returning the JSON representation of the User object post-update?

like image 274
goggin13 Avatar asked Feb 05 '13 20:02

goggin13


1 Answers

Good question, apparently the purpose is to return a HTTP status code 200 with an empty body, see this discussion. Maybe for brevity or security purposes. head :no_content seems to create a HTTP response 200 (success) with an empty body, returning this response header:

Status Code:200 OK

see also this related question.

like image 138
0x4a6f4672 Avatar answered Nov 06 '22 02:11

0x4a6f4672