Skip to content

Instantly share code, notes, and snippets.

@romeuhcf
Created December 17, 2019 13:53
Show Gist options
  • Save romeuhcf/bb29636524552b2eb2252a98e6590aaf to your computer and use it in GitHub Desktop.
Save romeuhcf/bb29636524552b2eb2252a98e6590aaf to your computer and use it in GitHub Desktop.
Multi verb http
require 'net/http'
require 'uri'
def call(method:, host:, path: '/', query_parameters: {})
method = method.to_s.downcase
method = method[0].upcase + method[1..]
uri = URI(File.join(host, path))
uri.query = URI.encode_www_form(query_parameters)
# TODO POST raw payload
# TODO POST form fields
# TODO request headers
Net::HTTP.start(uri.host, uri.port) do |http|
req = Net::HTTP.const_get(method).new(uri)
return http.request(req)
end
end
# testing....
res = call(method: :get,
host: 'http://example.com',
path: '/',
query_parameters: {a: 'b'})
p res.code, res.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment