Skip to content

Instantly share code, notes, and snippets.

@raorao
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raorao/8966135 to your computer and use it in GitHub Desktop.
Save raorao/8966135 to your computer and use it in GitHub Desktop.
#HTTParty isn't broken! it deals with 304 errors as per RFC 2616,
#returning a response with headers but no body.
require 'httparty'
response = HTTParty.get("https://api.github.com/users/octocat", {
:headers => {"User-Agent" => "HTTParty"},
})
etag = response.headers["etag"]
response = HTTParty.get("https://api.github.com/users/octocat", {
:headers => { "If-None-Match" => etag, "User-Agent" => "HTTParty"}
})
puts response
#nil
puts response.headers["date"]
# Wed, 12 Feb 2014 22:45:32 GMT
puts response.headers["etag"]
# "e0b2dfe317b38e9e8e2bb2a22fecb08f"
puts response.headers.inspect
# {"server"=>["GitHub.com"],
# "date"=>["Wed, 12 Feb 2014 22:45:32 GMT"],
# "connection"=>["close"],
# "status"=>["304 Not Modified"],
# "x-ratelimit-limit"=>["60"],
# "x-ratelimit-remaining"=>["35"],
# "x-ratelimit-reset"=>["1392247594"],
# "cache-control"=>["public, max-age=60, s-maxage=60"],
# "last-modified"=>["Wed, 12 Feb 2014 16:24:12 GMT"],
# "etag"=>["\"e0b2dfe317b38e9e8e2bb2a22fecb08f\""],
# "x-content-type-options"=>["nosniff"],
# "access-control-allow-credentials"=>["true"],
# "access-control-expose-headers"=>["ETag, Link, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval"],
# "access-control-allow-origin"=>["*"],
# "x-github-request-id"=>["{{SOME HEX}}"],
# "vary"=>["Accept-Encoding"]
# }
@johana-star
Copy link

Oh blast! I was thinking maybe I was an idiot and the response object might respond to headers even though there's no body.

(Because response is just an alias for response.body.)

@raorao
Copy link
Author

raorao commented Feb 13, 2014

I mean its a little strange that a body-less response would print nothing to the screen, but it does match the httparty gem specifications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment