Skip to content

Instantly share code, notes, and snippets.

@ygpark2
Last active August 29, 2015 14:02
Show Gist options
  • Save ygpark2/919da245d1950df07fd1 to your computer and use it in GitHub Desktop.
Save ygpark2/919da245d1950df07fd1 to your computer and use it in GitHub Desktop.
ruby http client file
def http_client(url, method, headers={}, body=nil)
require 'net/http'
uri = URI.parse("#{url}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == "https"
request = "Net::HTTP::#{method.capitalize}".constantize.new(uri.request_uri)
headers.keys.each do |key|
request[key] = headers[key]
end
if body.is_a?(Hash)
request.set_form_data(body)
else
request.body = body unless body.nil?
end
http.request(request)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment