1

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.

Snips
  • 6,575
  • 7
  • 40
  • 64
  • 1
    It's to work around a bug in Safari. Have a look at http://stackoverflow.com/questions/3351247/how-to-return-truly-empty-body-in-rails-i-e-content-length-0 – nickgrim Aug 16 '11 at 10:23
  • Ahh! Thank you, that explains it. If you want to add your comment as an answer, I'll mark it as such. Thanks again. – Snips Aug 16 '11 at 16:07

1 Answers1

2

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 :-)

Community
  • 1
  • 1
Snips
  • 6,575
  • 7
  • 40
  • 64
  • You may want to consider posting the full content of the link you referred to. That guarantees the solution's availability even if the target's page is no longer accessible. – Tass Jul 10 '15 at 16:40