Skip to content

Body is blank when response has no Content-Length #45

@bjeanes

Description

@bjeanes

As per RFC 2616 section 4.4, Content-Length is not a required header in responses. I'm using The HTTP Gem with a server that doesn't always send a Content-Length header.

In these cases, the body content is "". Combined with accept(:json), this usually means A JSON text must at least contain two octets! (JSON::ParserError) exception.

See how Curl handles this:

curl -i 'https://api.honeybadger.io/v1/projects.json?auth_token=a-bad-key' -H 'Accept: application/json'
HTTP/1.1 401 Unauthorized
Server: nginx/1.4.3
Date: Mon, 04 Nov 2013 21:48:29 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 401 Unauthorized
Strict-Transport-Security: max-age=31536000
X-UA-Compatible: IE=Edge,chrome=1
X-Request-Id: b48da36d18e3c259bfec15019f3393c4
X-Runtime: 0.039962
X-Rack-Cache: miss

{"error":"Invalid authentication token."}

With The HTTP Gem:

irb(main):001:0> require 'http'
=> true
irb(main):002:0> HTTP.get("https://api.honeybadger.io/v1/projects.json", params: {auth_token: "a-bad-key"})
=> ""

A gander at the code shows that this is because @body_remaining is derived entirely from the presence of a Content-Length header. In some (all?) conditions, if @body_remaining is nil, the body is not read.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions