kernification kernification - 4 months ago 18
Ruby Question

Transform curl to open-uri in ruby?

I have a curl line like this:

curl https://api.paymill.com/v2.1/checksums -u 'private_user_key' -d 'checksum_type=paypal' -d 'amount=4200'


and I want to transform it into a open-uri call like this:

require 'open-uri'
open("https://api.paymill.com/v2.1/checksums", http_basic_authentication: ['private_user_key'])


Works so far, but how do I include the -d data-attributes like -d 'checksum_type=paypal' -d 'amount=4200'?

Thanks, Andreas

Answer

I think you'll be better off using Net::HTTP directly (instead of open-uri, which is just a convenience wrapper). This gives you more control over the request being made.

require 'net/http'
uri = URI("https://api.paymill.com/v2.1/checksums")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.basic_auth("username", "password")
request.set_form_data('checksum_type' => 'paypal', 'amount' => 4200)
response = http.request(request)
Comments