Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

'null' and bodies made up out of spaces

The format. helper in Rails calls .to_json on the object in the body.
When using the head helper, it calls to_json on nil, resulting in
"null". Since its a null value, it isn't necessarilly incorrect, but its
debatable whether a HEAD only response should have a body at all.

On the other hand. MultiJSON doesn't parse "null", "  ", nil or an empty
string.

Of course we can also blame Net::HTTP for saying that null in the body
stands for a string with "null". Thats like having a barbeque,
forgetting the meat, but putting some lard on anyway.

So, lets just let HTTParty fix it. Everyone's invited :-)
  • Loading branch information...
commit 5a891c4273eb131e3eb6b748b6d8c4d032846446 1 parent 4ccc7bf
@timonv timonv authored
Showing with 12 additions and 2 deletions.
  1. +2 −2 lib/httparty/parser.rb
  2. +10 −0 spec/httparty/parser_spec.rb
View
4 lib/httparty/parser.rb
@@ -96,9 +96,9 @@ def initialize(body, format)
private_class_method :new
# @return [Object] the parsed body
- # @return [nil] when the response body is nil or an empty string
+ # @return [nil] when the response body is nil, an empty string, spaces only or "null"
def parse
- return nil if body.nil? || body.empty?
+ return nil if body.nil? || body.strip.empty? || body == "null"
if supports_format?
parse_supported_format
else
View
10 spec/httparty/parser_spec.rb
@@ -87,6 +87,16 @@ class MyParser < HTTParty::Parser
@parser.stub(:body => nil)
@parser.parse.should be_nil
end
+
+ it "returns nil for a 'null' body" do
+ @parser.stub(:body => "null")
+ @parser.parse.should be_nil
+ end
+
+ it "returns nil for a body with spaces only" do
+ @parser.stub(:body => " ")
+ @parser.parse.should be_nil
+ end
end
describe "#supports_format?" do
Please sign in to comment.
Something went wrong with that request. Please try again.