如何在ruby中解析此查询URL

时间:2014-08-19 01:37:38

标签: mysql ruby parsing

这是我在curl中的查询命令,我可以按预期得到结果

curl --data "action=search&user=wbt&project=Test&ltoken=&query=build:tables_tempkb status:Analyze" http://xxx.xxx.xxx:8088/review/api

但是,如果我尝试在ruby中查询

url = "http://xxx.xxx.xxx.xxx:8088/review/api/?action=search&user=wbt&project=Test&ltoken=&query=build:tables_tempkb status:Analyze"
uri = URI(url)
res = Net::HTTP.post_form(uri, 'q' => 'ruby')

ruby​​报告下面的解析错误

bad URI(is not URI?): http://xxx.xxx.xxx.xxx:8088/review/api/?action=search&user=wbt&project=Test&ltoken=&query=build:tables_tempkb status:Analyze
["C:/Ruby200/lib/ruby/2.0.0/uri/common.rb:176:in `split'", "C:/Ruby200/lib/ruby/2.0.0/uri/common.rb:211:in `parse'", "C:/Ruby200/lib/ruby/2.0.0/uri/common.rb:747:in `parse'", "C:/Ruby200/lib/ruby/2.0.0/uri/common.rb:996:in `URI'"

你能让我知道我的ruby脚本有什么问题吗?

2 个答案:

答案 0 :(得分:1)

将空间转换为%20。这有效:

url = "http://xxx.xxx.xxx.xxx:8088/review/api/?action=search&user=wbt&project=Test&ltoken=&query=build:tables_tempkb%20status:Analyze"
uri = URI(url)

或者,改为使用+

url = "http://xxx.xxx.xxx.xxx:8088/review/api/?action=search&user=wbt&project=Test&ltoken=&query=build:tables_tempkb+status:Analyze"
uri = URI(url)

两者都将作为空间在接收应用程序中解码。

请参阅Percent Encoding

答案 1 :(得分:0)

在解析之前,只需使用URI.encode对网址进行编码:

 url = "http://xxx.xxx.xxx.xxx:8088/review/api/?action=search&user=wbt&project=Test&ltoken=&query=build:tables_tempkb status:Analyze"
 uri = URI(URI.encode(url))