John Hawthorn

curl-to-ruby: Writing API wrappers the lazy way

This week I published curl-to-ruby, a tool to convert curl commands.

For example you can input:

curl http://www.example.com

and it will give you:

require 'net/http' require 'uri' uri = URI.parse("http://echoip.com") response = Net::HTTP.get_response(uri)

curl, due to it’s prevalence, is often used in API examples.

Both chrome and firefox have “copy to curl” as part of their network inspectors.

Copy as curl puts the following in the clipboard

curl 'https://gist.github.com/' \ -H 'Cookie: _octo=GH1.1.1344769836.1465267359; _gat=1; _gh_sess=eyJzZXNzaW9uX2l\ kIjoiZDJiN2I1NTQzMTZjNmE0YmI3MTk2NmNmMGY5MDMxZmMiLCJfY3NyZl90b2tlbiI6IlYxTjdUd2\ FQYmxRYmF5Rjh3Z0hXU0lBTVlrV3djM3p6TE5JdjRiWk5RMEE9IiwicmVmZXJyYWxfY29kZSI6Imh0d\ HBzOi8vZ2lzdC5naXRodWIuY29tLyIsImFub25fZ2lzdHMiOlsiMzY1MzgxMjQiXSwibGFzdF93cml0\ ZSI6MTQ2NTI2NzQ0OTEwNn0%3D--bd8c32842d30846c81ea28e9b52311a6e507444d; _ga=GA1.2\ .1962934996.1465267359; tz=America%2FVancouver' \ -H 'Origin: https://gist.github.com' \ -H 'Accept-Encoding: gzip, deflate, br' \ -H 'Accept-Language: en-US,en;q=0.8' \ -H 'Upgrade-Insecure-Requests: 1' \ -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 \ (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36' \ -H 'Content-Type: application/x-www-form-urlencoded' \ -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' \ -H 'Cache-Control: max-age=0' \ -H 'Referer: https://gist.github.com/' \ -H 'Connection: keep-alive' \ --data 'utf8=%E2%9C%93&authenticity_token=2raC0Huj0rU6JuAaGrGEHW2g7C4D7uZt9p5Cs\ UWzxHnXq9Sf63dyx9FnJiIzL3fBuBkYxDsokTM3uqfJDDqJ6A%3D%3D&gist%5Bdescription%5D=d\ escription+goes+here&gist%5Bcontents%5D%5B%5D%5Boid%5D=&gist%5Bcontents%5D%5B%5\ D%5Bname%5D=filename+goes+here&new_filename=filename+goes+here&content_changed=\ true&gist%5Bcontents%5D%5B%5D%5Bvalue%5D=body+goes+here&gist%5Bpublic%5D=1' \ --compressed

Putting this into curl-to-ruby gives you

require 'net/http'
require 'uri'

uri = URI.parse("https://gist.github.com/")
request = Net::HTTP::Post.new(uri)
request.content_type = "application/x-www-form-urlencoded"
request["Cookie"] = "_octo=GH1.1.1344769836.1465267359; _gat=1; _gh_sess=eyJzZXNzaW9uX2lkIjoiZDJiN2I1NTQzMTZjNmE0YmI3MTk2NmNmMGY5MDMxZmMiLCJfY3NyZl90b2tlbiI6IlYxTjdUd2FQYmxRYmF5Rjh3Z0hXU0lBTVlrV3djM3p6TE5JdjRiWk5RMEE9IiwicmVmZXJyYWxfY29kZSI6Imh0dHBzOi8vZ2lzdC5naXRodWIuY29tLyIsImFub25fZ2lzdHMiOlsiMzY1MzgxMjQiXSwibGFzdF93cml0ZSI6MTQ2NTI2NzQ0OTEwNn0%3D--bd8c32842d30846c81ea28e9b52311a6e507444d; _ga=GA1.2.1962934996.1465267359; tz=America%2FVancouver"
request["Origin"] = "https://gist.github.com"
request["Accept-Language"] = "en-US,en;q=0.8"
request["Upgrade-Insecure-Requests"] = "1"
request["User-Agent"] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36"
request["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
request["Cache-Control"] = "max-age=0"
request["Referer"] = "https://gist.github.com/"
request["Connection"] = "keep-alive"
request.set_form_data(
  "utf8" => "✓",
  "authenticity_token" => "2raC0Huj0rU6JuAaGrGEHW2g7C4D7uZt9p5CsUWzxHnXq9Sf63dyx9FnJiIzL3fBuBkYxDsokTM3uqfJDDqJ6A==",
  "gist[description]" => "description goes here",
  "gist[contents][][oid]" => "",
  "gist[contents][][name]" => "filename goes here",
  "new_filename" => "filename goes here",
  "content_changed" => "true",
  "gist[contents][][value]" => "body goes here",
  "gist[public]" => "1",
)

response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
  http.request(request)
end

Stripping this down a bit

require 'net/http'
require 'uri'

uri = URI.parse("https://gist.github.com/")
request = Net::HTTP::Post.new(uri)
request.content_type = "application/x-www-form-urlencoded"
request["Cookie"] = "_octo=GH1.1.1344769836.1465267359; _gat=1; _gh_sess=eyJzZXNzaW9uX2lkIjoiZDJiN2I1NTQzMTZjNmE0YmI3MTk2NmNmMGY5MDMxZmMiLCJfY3NyZl90b2tlbiI6IlYxTjdUd2FQYmxRYmF5Rjh3Z0hXU0lBTVlrV3djM3p6TE5JdjRiWk5RMEE9IiwicmVmZXJyYWxfY29kZSI6Imh0dHBzOi8vZ2lzdC5naXRodWIuY29tLyIsImFub25fZ2lzdHMiOlsiMzY1MzgxMjQiXSwibGFzdF93cml0ZSI6MTQ2NTI2NzQ0OTEwNn0%3D--bd8c32842d30846c81ea28e9b52311a6e507444d; _ga=GA1.2.1962934996.1465267359; tz=America%2FVancouver"
request["Origin"] = "https://gist.github.com"
request["Accept-Language"] = "en-US,en;q=0.8"
request["Upgrade-Insecure-Requests"] = "1"
request["User-Agent"] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36"
request["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
request["Cache-Control"] = "max-age=0"
request["Referer"] = "https://gist.github.com/"
request["Connection"] = "keep-alive"
request.set_form_data(
  "utf8" => "✓",
  "authenticity_token" => "2raC0Huj0rU6JuAaGrGEHW2g7C4D7uZt9p5CsUWzxHnXq9Sf63dyx9FnJiIzL3fBuBkYxDsokTM3uqfJDDqJ6A==",
  "gist[description]" => "description goes here",
  "gist[contents][][oid]" => "",
  "gist[contents][][name]" => "filename goes here",
  "new_filename" => "filename goes here",
  "content_changed" => "true",
  "gist[contents][][value]" => "body goes here",
  "gist[public]" => "1",
)

response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
  http.request(request)
end