Search code examples
ruby-on-railshttppuma

Strange extra characters in Rails response body


I'm running rails application with Puma server and every response has some hex numbers at the start (looks like body length) and the end (always zero) of each response.

HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-UA-Compatible: chrome=1
Content-Type: application/json; charset=utf-8
ETag: "8e6d795d26a3839c23c0b28f7b2e3c08"
Cache-Control: max-age=0, private, must-revalidate
Set-Cookie: [skip]; path=/; HttpOnly
X-Request-Id: 741cdbde-39a8-4b99-9af7-15a7e65ff1ac
X-Runtime: 0.202731
Transfer-Encoding: chunked

f <-- THIS
{"response":{}}
0 <-- AND THIS

What is this?


Solution

  • That's the way chunked transfer encoding works. It sends the body in pieces, preceded by a line containing the length of the piece in hexadecimal. The end of the body is indicated by a chunk with 0 length.

    So what you're showing is that the first chunk is 15 characters long, and there's no second chunk.

    A proper HTTP client or API should strip these chunk headers out, and just return the contents.

    See Wikipedia for more details, or RFC 2616 for the official spec.