Ruby HTTP获取参数 [英] Ruby HTTP get with params
问题描述
如何通过ruby发送HTTP GET请求参数?
我尝试了很多示例,但都失败了。
我知道这篇文章是旧的,但为了这些由谷歌带来的人,有一种更简单的方法来以URL安全的方式编码您的参数。我不确定为什么我没有在其他地方看到这个方法,因为这个方法在Net :: HTTP页面上有记录。我已经看到了Arsen7所描述的方法也被认为是其他几个问题的答案。
在 Net :: HTTP 文档是 URI.encode_www_form(params)$ c
#可以说我们有一个路径和参数,如下所示:
path =/搜索
params = {q:=> 答案}
#示例1:替换Arsen7中的#path_with_params方法
def path_with_params(path,params)
encoded_params = URI.encode_www_form(params)
[path,encoded_params] .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_params = URI.encode_www_form(params)
[路径,encoded_params] .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屋!