Ruby HTTP获取参数 [英] Ruby HTTP get with params

查看:121
本文介绍了Ruby HTTP获取参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过ruby发送HTTP GET请求参数?



我尝试了很多示例,但都失败了。

解决方案

我知道这篇文章是旧的,但为了这些由谷歌带来的人,有一种更简单的方法来以URL安全的方式编码您的参数。我不确定为什么我没有在其他地方看到这个方法,因为这个方法在Net :: HTTP页面上有记录。我已经看到了Arsen7所描述的方法也被认为是其他几个问题的答案。

Net :: HTTP 文档是 URI.encode_www_form(params)

 #可以说我们有一个路径和参数,如下所示:
path =/搜索
params = {q:=> 答案}

#示例1:替换Arsen7中的#path_with_params方法
def path_with_params(path,params)
encoded_pa​​rams = URI.encode_www_form(params)
[path,encoded_pa​​rams] .join(?)
end

#示例2:整个Arsen7示例的快捷方式
uri = URI.parse(http: //localhost.com+ path)
uri.query = URI.encode_www_form(params)
response = Net :: HTTP.get_response(uri)

您选择的示例非常依赖于您的用例。在我目前的项目中,我使用了类似于Arsen7推荐的方法,以及更简单的 #path_with_params 方法,并且没有块格式。

 #没有响应的简单示例实现
#解码或错误处理。

需要净/ http
需要uri

类连接
VERB_MAP = {
:get => Net :: HTTP :: Get,
:post => Net :: HTTP :: Post,
:put => Net :: HTTP :: Put,
:delete => Net :: HTTP :: Delete
}

API_ENDPOINT =http://dev.random.com

attr_reader:http

def initialize(endpoint = API_ENDPOINT)
uri = URI.parse(endpoint)
@http = Net :: HTTP.new(uri.host,uri.port)
end

def请求(方法,路径,参数)
大小写方法
当:get
full_path = path_with_params(路径,参数)
请求= VERB_MAP [方法] .new(full_path)
else
request = VERB_MAP [method] .new(path)
request.set_form_data(params)
end

http。请求(请求)
结束

私有

def path_with_params(路径,参数)
encoded_pa​​rams = URI.encode_www_form(params)
[路径,encoded_pa​​rams] .join(?)
结束

结束

con = Connection.new
con.request(:post, / account,{:email =>test@test.com})
=> #< Net :: HTTPCreated 201 Created readbody = true>


How can I send HTTP GET request with parameters via ruby?

I have tried a lot of examples but all of those failed.

解决方案

I know this post is old but for the sake of those brought here by google, there is an easier way to encode your parameters in a URL safe manner. I'm not sure why I haven't seen this elsewhere as the method is documented on the Net::HTTP page. I have seen the method described by Arsen7 as the accepted answer on several other questions also.

Mentioned in the Net::HTTP documentation is URI.encode_www_form(params):

# Lets say we have a path and params that look like this:
path = "/search"
params = {q: => "answer"}

# Example 1: Replacing the #path_with_params method from Arsen7
def path_with_params(path, params)
   encoded_params = URI.encode_www_form(params)
   [path, encoded_params].join("?")
end

# Example 2: A shortcut for the entire example by Arsen7
uri = URI.parse("http://localhost.com" + path)
uri.query = URI.encode_www_form(params)
response = Net::HTTP.get_response(uri)

Which example you choose is very much dependent on your use case. In my current project I am using a method similar to the one recommended by Arsen7 along with the simpler #path_with_params method and without the block format.

# Simplified example implementation without response
# decoding or error handling.

require "net/http"
require "uri"

class Connection
  VERB_MAP = {
    :get    => Net::HTTP::Get,
    :post   => Net::HTTP::Post,
    :put    => Net::HTTP::Put,
    :delete => Net::HTTP::Delete
  }

  API_ENDPOINT = "http://dev.random.com"

  attr_reader :http

  def initialize(endpoint = API_ENDPOINT)
    uri = URI.parse(endpoint)
    @http = Net::HTTP.new(uri.host, uri.port)
  end

  def request(method, path, params)
    case method
    when :get
      full_path = path_with_params(path, params)
      request = VERB_MAP[method].new(full_path)
    else
      request = VERB_MAP[method].new(path)
      request.set_form_data(params)
    end

    http.request(request)
  end

  private

  def path_with_params(path, params)
    encoded_params = URI.encode_www_form(params)
    [path, encoded_params].join("?")
  end

end

con = Connection.new
con.request(:post, "/account", {:email => "test@test.com"})
=> #<Net::HTTPCreated 201 Created readbody=true> 

这篇关于Ruby HTTP获取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆