Parametrized get request in Ruby?

后端 未结 7 1404
北恋
北恋 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:27

    require 'net/http' require 'uri'
    
    uri = URI.parse( "http://www.google.de/search" ); params = {'q'=>'cheese'}
    
    http = Net::HTTP.new(uri.host, uri.port) 
    request = Net::HTTP::Get.new(uri.path) 
    request.set_form_data( params )
    
    # instantiate a new Request object
    request = Net::HTTP::Get.new( uri.path+ '?' + request.body ) 
    
    response = http.request(request)
    puts response.body
    

    I would expect it to work without the second instantiation as it would be the first request-objects or the http-object's job but it worked for me this way.

提交回复
热议问题