ruby net/http模块使用


ruby中的NET::HTTP;这里暂时先列出几个固定用法:

其中一,二不支持请求头设置(header取ruby默认值),只能用于基本的请求,不支持持久连接,如果您执行许多HTTP请求,则不推荐它们;三,四可以设置请求头;

NET::HTTP不能处理重定向和404 ;不支持会话保持

一. 基本GET请求get_response

require 'net/http'

# 基本GET请求
require 'net/http'
uri = URI('http://httpbin.org/get')
response = Net::HTTP.get_response(uri)
puts response.body


# 带参数的GET请求
require 'net/http'
uri = URI('http://httpbin.org/get?name=zhaofan&age=23')
response = Net::HTTP.get_response(uri)
puts response.body

# 如果我们想要在URL查询字符串传递数据,通常我们会通过httpbin.org/get?key=val方式传递
# URI模块允许使用 encode_www_form 方法,将一个HASH来进行转化,例子如下:
require 'net/http'
uri = URI('http://httpbin.org/get?')
data = {name:'zhaofan', age:23}
uri.query = URI.encode_www_form(data)
response = Net::HTTP.get_response(uri)
puts response.body

 

二.基本post请求post_form(header默认为application/x-www-form-urlencoded)

require 'net/http'

# 基本POST请求
# 通过在发送post请求时添加一个params参数,这个params参数可以通过字典构造成;
# 该方法底层和get_response一样都是调用了response方法
uri = URI('http://httpbin.org/post')
params = {name:'zhaofan', age:23}
res = Net::HTTP.post_form(uri, params)
puts res.body

 

三.get固定用法

require 'net/http'

url = URI('http://httpbin.org/get?')
# 设置请求参数
params = {'name':'test'}
url.query = URI.encode_www_form(params)
http = Net::HTTP.new(url.host, url.port)
# 设置请求头
header = {'user-agent':'Chrome/61.0.3163.100'}

response = http.get(url, header)
puts response.body

 

四.post固定用法

  • application/json
    require 'net/http'
    require 'json'
    
    #application/json
    url = URI('http://httpbin.org/post')
    
    http = Net::HTTP.new(url.host, url.port)
    # 设置请求参数
    data = {'code':1, 'sex':'', 'id':1900, 'name': 'test'}.to_json
    # 设置请求头
    header = {'content-type':'application/json'}
    response = http.post(url, data, header)
    puts response.body

     

  • application/x-www-form-urlencoded
    require 'net/http'
    require 'json'
    
    #application/x-www-form-urlencoded
    url = URI('http://httpbin.org/post')
    http = Net::HTTP.new(url.host, url.port)
    #encodeURI参数
    data = URI.encode_www_form({'name':'test'})
    #设置请求头
    header = {'content-type':'application/x-www-form-urlencoded'}
    response = http.post(url, data,header)
    puts response.body

     

  • multipart/form-data 上传文件
    # multipart/form-data
    url = URI('http://httpbin.org/post')
    http = Net::HTTP.new(url.host, url.port)
    
    # 设置请求参数
    data = [
        ['name', 'test'],
        ['image', open('test.jpg'), { filename: 'test.jpg'}]
    ]
    request = Net::HTTP::Post.new(url.path)
    request.set_form(data, 'multipart/form-data')
    response = http.request(request)
    puts response.body

     

 五.response响应内容

require 'net/http'
uri = URI('http://www.baidu.com')
response = Net::HTTP.get_response(uri)
puts "http版本:#{response.http_version}" #=> 1.1
puts "响应代码: #{response.code}" #=> 200
puts "响应信息:#{response.message}" #=> ok
puts "uri:#{response.uri}" #=> http://www.baidu.com
puts "解码信息:#{response.decode_content}" #=> true
puts response.body #=> 响应体
response.header.each do |k,v|
  puts "#{k}:#{v}"
end #=> 响应头

 

六.发送https请求

require 'net/https'
# 设置https
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

 

转载于:https://www.cnblogs.com/wf0117/p/9000443.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Started GET "/notebooks/" for 127.0.0.1 at 2023-07-14 09:59:56 +0800 Processing by NotebooksController#index as HTML Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms) NoMethodError (undefined method `all' for Notebook:Module): app/controllers/notebooks_controller.rb:4:in `index' Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.2ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.1ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.5ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/actionpack-4.2.11.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (9.8ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.2ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.1ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (8.4ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.1ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.2ms) Rendered /home/meiyi/.asdf/installs/ruby/2.6.9/lib/ruby/gems/2.6.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (15.1ms)
07-15

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值