Created
December 17, 2019 13:53
-
-
Save romeuhcf/bb29636524552b2eb2252a98e6590aaf to your computer and use it in GitHub Desktop.
Multi verb http
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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