Ruby sends https request post and get in two ways

The content of the article is personal study notes. If there is any mistake, I hope you will criticize and correct it. I am grateful.

Documents to be imported

require 'json'
require 'net/https'
require 'open-uri'

post method

#url:对应的https请求
#toSend:对应的参数{:name => 'lzy'} 形式
   def send_post(url,toSend)
	uri = URI(url)
	http = Net::HTTP.new(uri.host, uri.port)
		if uri.scheme == "https"
 			http.use_ssl = true
  			http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
		end
	req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
	req.set_form_data(toSend)
	res = http.request(req)
	return JSON.parse res.body
    end

get method

def send_get
	name="lzy"
	uri = URI.parse('https://lzyu,club?name='+name)
	http = Net::HTTP.new(uri.host, uri.port)
		if uri.scheme == "https"
 			http.use_ssl = true #这个很关键
  			http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
		end
	http.start {
      http.request_get(uri.path) {|res|
      return JSON.parse res.body
       }
     }
end 

These two methods are to use the files that come with ruby ​​to send

Insert picture description herePersonal interest station, with information attached~

Guess you like

Origin blog.csdn.net/weixin_42656358/article/details/99309748