Skip to content

Instantly share code, notes, and snippets.

@buyoh
Created March 15, 2021 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buyoh/2e3d0592f6206f82d962219dd11da448 to your computer and use it in GitHub Desktop.
Save buyoh/2e3d0592f6206f82d962219dd11da448 to your computer and use it in GitHub Desktop.
easy snippet (HTTP,HTTPS,get,post,put,delete,...)
require 'net/http'
def action_http(uri_str, metho = 'get', field = {}, body = nil, content_type = 'application/json')
uri = URI.parse(uri_str)
req = Net::HTTP.const_get(metho.capitalize).new(uri)
field.each { |k, v| req.add_field(k, v) }
if body
req.add_field 'Content-Type', content_type
req.body = body
end
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == 'https'
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
res = http.start do |http|
http.request(req)
end
[res.code, res.body]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment