Parametrized get request in Ruby?

后端 未结 7 1292
北恋
北恋 2020-12-04 17:54

How do I make an HTTP GET request with parameters in Ruby?

It\'s easy to do when you\'re POSTing:

  require \'net/http\'
           


        
相关标签:
7条回答
  • 2020-12-04 18:40

    Use the following method:

    require 'net/http'
    require 'cgi'
    
    def http_get(domain,path,params)
        return Net::HTTP.get(domain, "#{path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&'))) if not params.nil?
        return Net::HTTP.get(domain, path)
    end
    
    params = {:q => "ruby", :max => 50}
    print http_get("www.example.com", "/search.cgi", params)
    
    0 讨论(0)
提交回复
热议问题