Snips
Snips

Reputation: 6773

HTTP POST, head :ok returning 1 mystery byte

I have a Rails application accessed via a mobile App - and the data is exchanged in JSON format.

When I perform a successful POST, I expect, and get, a HTTP code 200 OK returned. What I don't expect is an accompanying 1 byte of data ASCII 0x20 (i.e. a space).

I have the following code to return from the POST in the case where the object (device) being POSTed already exists.

    # Device is already registered, so update attributes of existing record (incl. device token)

    if @deviceFound.update_attributes(params[:device])
      format.html { redirect_to(@deviceFound, :notice => 'Device was successfully updated.') }
      format.xml  { head :ok }
      # format.json { head :ok }
      format.json do
        render :nothing => true, :status => :ok
        return true
      end
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @deviceFound.errors, :status => :unprocessable_entity }
      format.json { render :json => @deviceFound.errors, :status => :unprocessable_entity }
    end

From the commented out line, you'll see that I have been using format.json { head :ok } but in an attempt to understand why I'm getting this byte returned, I've tried the alternative implementation which I believe to be equivalent. Both yield the same results HTTP 200 + 1 byte of data.

btw, if I filter out the 1 byte in this case, in all other cases my mobile App interacts with the Rails App just fine.

I'd appreciate it if someone could explain why I'm getting a byte of data in the response?

Thank you.

Upvotes: 1

Views: 2531

Answers (1)

Snips
Snips

Reputation: 6773

Kudos to @nickgrim for answering this question, but just to close this off, the explanation is available here...

How to return truly empty body in rails? i.e. content-length 0

Good to know I wasn't going mad :-)

Upvotes: 2

Related Questions