Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default_params only works for GET requests #33

Closed
rawsyntax opened this issue Feb 8, 2010 · 2 comments
Closed

default_params only works for GET requests #33

rawsyntax opened this issue Feb 8, 2010 · 2 comments

Comments

@rawsyntax
Copy link

It'd be nice if default params did the same functionality for POST requests, ie add the default params to the body instead of the query_string.

@jgn
Copy link

jgn commented May 4, 2010

I agree. Or another strategy, such as setting default_body_params

For example, ping.fm's REST service (http://groups.google.com/group/pingfm-developers/web/api-documentation?_done=%2Fgroup%2Fpingfm-developers%3F) requires API and "user" keys on all requests, and they must be posted. Big drag to have to keep adding those params.

@jgn
Copy link

jgn commented May 4, 2010

Here's a monkey patch . . .

module HTTParty
  module ClassMethods
    def default_body_params(h={})
      raise ArgumentError, 'Default body params must be a hash' unless h.is_a?(Hash)
      default_options[:default_body_params] ||= {}
      default_options[:default_body_params].merge!(h)
    end
  end
  class Request
    def body
      if options[:default_body_params]
        if options[:body]
          if options[:body].is_a?(Hash)
            options[:body] = options[:default_body_params].merge(options[:body])
          end
        else
          options[:body] = options[:default_body_params]
        end
      end
      if options[:body].is_a?(Hash)
        options[:body].to_params
      else
        options[:body]
      end
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants