Parametrized get request in Ruby?

后端 未结 7 1407
北恋
北恋 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:23

    new to stack overflow and I guess I can't comment, but @chris.moose is missing double quotes in his function def. line 5 should be:

    return Net::HTTP.get(domain, "#{path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.reverse.join('&'))) if not params.nil?
    

    or here's the whole thing redefined for copy/pasting

    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)}" }.reverse.join('&'))) if not params.nil?
        return Net::HTTP.get(domain, path)
    end
    

    <3 -mike

提交回复
热议问题