httprb / http

HTTP (The Gem! a.k.a. http.rb) - a fast Ruby HTTP client with a chainable API, streaming support, and timeouts
MIT License
3.01k stars 321 forks source link

Allow passing HTTP::FormData object to HTTP::Client directly #599

Closed summera closed 4 years ago

summera commented 4 years ago

As discussed with @ixti in https://github.com/httprb/form_data/pull/29, this allows you to pass an HTTP::FormData object directly instead of a Hash for more customization such as using a custom encoder. Example:

custom_encoder = proc { |data| ::JSON.dump(data) }
data = { "foo[bar]" => "test" }
form_data = HTTP::FormData.create(data, encoder: custom_encoder)
HTTP.post("https://some-url.com", form: form_data)

Note that I decided to use the following logic to determine whether an HTTP::FormData object was being passed in directly:

    def form_data(form)
      (form || {}).respond_to?(:to_h) ? HTTP::FormData.create(form) : form
    end

The other option would be to check if form is_a? HTTP::FormData::Urlencoded or a HTTP::FormData::Multipart but this seemed less flexible since it creates a coupling with what's returned from HTTP::FormData.create. However, I don't have a strong opinion here and am happy to change if another method is more desirable.

summera commented 4 years ago

@ixti provided everything here is all good, can we also backport this to v4.x?

summera commented 4 years ago

@ixti just wanted to check back with you on this. Let me know what you think.

ixti commented 4 years ago

Honestly I think it's better to check type. But I agree that checking multiple types in here is kinda ugly. So I tend to think that we can add coerce to FormData instead and use it here instead of create. WDYT?

summera commented 4 years ago

@ixti thanks for the merge and apologies for not getting back to you earlier with my thoughts! Any chance of backporting this update to 4.x?

summera commented 4 years ago

Sorry, just realized it already was! Great 👍 . Please let me know when a new release is out with the update 😄

ixti commented 4 years ago

Released v4.4.0