Created
March 15, 2021 16:03
-
-
Save buyoh/2e3d0592f6206f82d962219dd11da448 to your computer and use it in GitHub Desktop.
easy snippet (HTTP,HTTPS,get,post,put,delete,...)
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' | |
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