Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Set content-type headers for requests #96

wants to merge 1 commit into from

2 participants

Tinco Andringa John Nunemaker
Tinco Andringa

This commit is related to #30 (comment) issue #30

I have used this extensively, but that was over a year ago and that project was finished under time pressure so I have not added any unit tests for httparty itself and this might be too simple of a work around.

John Nunemaker

If you could add some tests to verify we don't break it in the future and a bit of reasoning I would be happy to pull. Sorry for the slow response.

John Nunemaker

Closing. Please re-open if you add tests. Thanks!

John Nunemaker jnunemaker closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 17, 2011
  1. Tinco Andringa
This page is out of date. Refresh to see the latest.
Showing with 16 additions and 1 deletion.
  1. +16 −1 lib/httparty/request.rb
17 lib/httparty/request.rb
@@ -149,7 +149,12 @@ def query_string_normalizer
def setup_raw_request
@raw_request =
- @raw_request.body = body if body
+ if body
+ @raw_request.body = body
+ if not body.is_a?(Hash)
+ options[:headers].merge!('content-type' => mimetype_from_format(format))
+ end
+ end
@raw_request.basic_auth(username, password) if options[:basic_auth]
setup_digest_auth if options[:digest_auth]
@@ -235,6 +240,16 @@ def format_from_mimetype(mimetype)
+ # Determines the mimetype from the format
+ # TODO support more formats automatically
+ def mimetype_from_format(format)
+ case format
+ when :xml : 'text/xml'
+ when :json : 'text/json'
+ when :html : 'text/html'
+ end
+ end
def validate
raise, 'HTTP redirects too deep' if options[:limit].to_i <= 0
raise ArgumentError, 'only get, post, put, delete, head, and options methods are supported' unless SupportedHTTPMethods.include?(http_method)
Something went wrong with that request. Please try again.