SoFunction
Updated on 2025-04-07

Simple example of sending HTTP-based requests in Ruby program

1. Establish an HTTP connection (send request parameters through GET)

require "open-uri"  
#If there is a GET request parameter written directly in the URI addressuri = 'http://uri'  
html_response = nil  
open(uri) do |http|  
html_response =   
end  
puts html_response  
require "open-uri" 
#If there is a GET request parameter written directly in the URI addressuri = 'http://uri' 
html_response = nil 
open(uri) do |http| 
html_response =  
end 
puts html_response 

2. Send request parameters through POST

params = {}  
params["name"] = 'Tom'  
uri = ("http://uri")  
res = Net::HTTP.post_form
(uri, params)  
#Returned cookiesputs ['set-cookie']  
#Returned html bodyputs   
params = {} 
params["name"] = 'Tom' 
uri = ("http://uri") 
res = Net::HTTP.post_form
(uri, params)  
#Returned cookiesputs ['set-cookie'] 
#Returned html bodyputs  

ask

# 
# describe:# Send express data to datasystem, use https# Enter:# data - data of assembled expes#Output:# Status information returned by datasystem# 
def (url,data) 
 url = url + data 
 $(url) 
 begin 
  uri = ((url)) 
  http = Net::(, ) 
  http.use_ssl = true 
   
  if ($logger != nil) 
   $("Link address parameters:#{(url)}, file name: #{__FILE__}, line #{__LINE__}")   $("Incomingdataparameter:#{data.to_json}, file name: #{__FILE__}, line #{__LINE__}")  end 
  request = Net::HTTP::(uri.request_uri) 
   
  response = (request) 
 rescue =>exception 
  $("transferurlAddress is#{url}, error!#{exception.to_s}, file name: #{__FILE__}, line #{__LINE__}")  return nil 
 end 
 return  
end