Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Set content-type headers for requests #96

Closed
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
Owner

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
Owner

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
View
@@ -149,7 +149,12 @@ def query_string_normalizer
def setup_raw_request
@raw_request = http_method.new(uri.request_uri)
- @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.initialize_http_header(options[:headers])
@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)
end
end
+ # 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 HTTParty::RedirectionTooDeep.new(last_response), '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.