require 'net/https'
require 'uri'
require 'json'
#请求参数
params = {
"name":"kegu",
"age":101
}
#请求头
header = {'content-type':'application/json'}
#get方式
def http_get(base_path,params,header)
base_path = URI(base_path)
base_path.query = URI.encode_www_form(params)
http = Net::HTTP.new(base_path.host,base_path.port)
return http.get(base_path,header)
end
#post方式
def http_post (base_path,params,header)
base_path = URI(base_path)
params=params.to_json
http = Net::HTTP.new(base_path.host,base_path.port)
return http.post(base_path, params,header)
end
#调用请求
res = http_post('http://localhost:3000/api/mxl/mxl_post',params,header)
# res = http_get('http://localhost:3000/api/mxl/mxl_get',params,header)
#响应内容
response = {
"http版本":res.http_version,
"响应代码":res.code,
"响应消息":res.message,
"uri":res.uri,
"解码信息":res.decode_content
}
res.header.each do |k,v|
response[k] = v
end
#输出
puts "-----消息头-----"
puts response
puts "-----消息体-----"
puts res.body