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 with query hash #11

Closed
maletor opened this issue Nov 12, 2011 · 10 comments · Fixed by #30
Closed

default_params with query hash #11

maletor opened this issue Nov 12, 2011 · 10 comments · Fixed by #30

Comments

@maletor
Copy link

maletor commented Nov 12, 2011

class API
  include HTTMultiParty

  default_params :XSC => 'system'
  format :xml
  headers 'User-Agent' => "gem"
  debug_output

  def self.post_stuff
    post("https://google.com/", :query => { :file => File.new("#{Rails.root}/tmp/BosJC.png") })
  end
end

API.post_stuff

Returns bad stuff

opening connection to google.com...
opened
opening connection to google.com...
opened
<- "POST /?XSC&system ...................
class API
  include HTTMultiParty

  default_params :XSC => 'system'
  format :xml
  headers 'User-Agent' => "gem"
  debug_output

  def self.post_stuff
    post("https://google.com/", :file => File.new("#{Rails.root}/tmp/BosJC.png"))
  end
end

API.post_stuff

Returns good stuff

opening connection to google.com...
opened
opening connection to google.com...
opened
<- "POST /?XSC=system ...................
@jwagener
Copy link
Collaborator

Sorry I couldn't get to that one yet. Hope to tackle it next week.

@maletor
Copy link
Author

maletor commented Dec 20, 2011

Any luck with this one?

@did
Copy link

did commented Oct 16, 2012

hey all,
I don't know exactly what the bug comes from but looks like the multipart request does not like params passed in the url like the ones coming from the default_params attribute of the class. That patch moves them into the body.
https://gist.github.com/3902187
It runs fine in my application.
Hope it help you @maletor

@more-ron
Copy link

This got me too.

Thanks for the patch @did.

@mattbearman
Copy link

I'm trying to use this gem with the ActiveCollab API, and it seems certain params need to be in the url (api_auth_token and path_info), and not in the body, meaning I'm unable to upload files.

@maletor
Copy link
Author

maletor commented Mar 20, 2014

Sorry to hear that. For now I've abandoned this gem due to this issue and gone with plain old HTTParty, Net::HTTP and Net::HTTP::Multipart. Here is my workaround.

def method(files)
  query = default_params
  query.update(
    unique_param: "value"
  )

  uri = URI.parse(base_uri)
  uri.path = "/api/path"
  uri.query = URI.encode_www_form(query)

  uploads = convert_to_upload_io(files)
  request = Net::HTTP::Post::Multipart.new(uri.request_uri, file: uploads)
  request.add_field('User-Agent', "blah gem #{VERSION}")

  response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    http.read_timeout = default_options[:timeout]
    http.open_timeout = default_options[:timeout]
    http.request(request)
  end

  JSON.parse(response.body)
end

@radar
Copy link
Contributor

radar commented Mar 21, 2014

@maletor First of all: eew.

Second of all: What fortunate timing! I've been needing to make multi-part HTTP requests too and I've stumbled across this gem AND this issue. "Wow, an issue that's TWO YEARS OLD! That must be really damn hard to fix."

5 minutes later: https://github.com/radar/httmultiparty/commit/d65d5904fbc4cc08db7b1bc5d034cd5ea9ac201e

2 minutes after that: https://github.com/radar/httmultiparty/commit/97da08e9c29223bc91973913b0d2fc9fa879f75d

So yeah. This issue that's been open on this project for two frickin' years is now fixed. Go ahead and use my version if you want.

Please merge the below PR, @jwagener.

@maletor
Copy link
Author

maletor commented Mar 21, 2014

Nicely done, @radar.

@prathamesh-sonpatki
Copy link
Collaborator

@radar Looking at it. Will merge soon

@prathamesh-sonpatki
Copy link
Collaborator

@maletor @radar Try latest version 0.3.13.

Thanks.

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

Successfully merging a pull request may close this issue.

7 participants