麻烦将params传递给GET请求 - Ruby脚本

时间:2015-08-21 18:29:47

标签: ruby

我有一个Ruby脚本向一个宁静的API发出GET请求,但是它忽略了我试图传入的参数。我想获得激活的用户,但它返回 all 用户。

我没有正确地传递我的参数吗?这是我的剧本:

require 'net/http'
require 'net/https'
require 'time'
require 'api-auth'
require 'json'

URL = 'https://<instance name>.mingle-api.thoughtworks.com/api/v2/users.xml'
OPTIONS = {:access_key_id => '<sign in name>', :access_secret_key => '<secret key>'}
PARAMS = {:user => { :activated => true } }

def http_get(url, options={}, params)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  body = params.to_json

  request = Net::HTTP::Get.new(uri.request_uri)
  request.body = body
  request['Content-Type'] = 'application/json'
  request['Content-Length'] = body.bytesize

  ApiAuth.sign!(request, options[:access_key_id], options[:access_secret_key])

  response = http.request(request)
  users = response.body

  if response.code.to_i > 300
    raise StandardError, <<-ERROR
    Request URL: #{url}
    Response: #{response.code}
    Response Message: #{response.message}
    Response Headers: #{response.to_hash.inspect}
    Response Body: #{response.body}
    ERROR
  end
  puts users
end

http_get(URL, OPTIONS, PARAMS)

响应是用户的XML,格式如下:

<user>
<id type="integer">2228</id>
<name>NAME</name>
<login>example@example.com</login>
<email>example@example.com</email>
<light type="boolean">false</light>
<icon_path nil="true"></icon_path>

<activated type="boolean">true</activated>
<admin type="boolean">false</admin>
</user>

在编码时我还是初学者,所以非常感谢任何帮助!谢谢!

2 个答案:

答案 0 :(得分:0)

当你将它们放在URL中时,你将把get请求的参数放在正文中,这样URL的末尾看起来像这样:

?param1=value1&param2=value2

使用这样的函数(source):

require 'uri'
def hash_to_query(hash)
  return URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&"))
end

创建URI时:

uri = URI.parse("#{url}?#{hash_to_query(params)}")

答案 1 :(得分:0)

通过URL传递参数对我来说不起作用。

我最终从返回的XML中提取数据,而不是缩小搜索范围。这将返回正确的数据:

require 'net/http'
require 'net/https'
require 'time'
require 'api-auth'
require 'json'
require 'nokogiri'

URL = 'https://<instance name>.mingle-api.thoughtworks.com/api/v2/users.xml'
OPTIONS = {:access_key_id => '<sign in name>', :access_secret_key => '<secret key>'}

def http_get(url, options={})
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Get.new(uri.request_uri)
  ApiAuth.sign!(request, options[:access_key_id], options[:access_secret_key])

  response = http.request(request)
  users = response.body

  if response.code.to_i > 300
    raise StandardError, <<-ERROR
    Request URL: #{url}
    Response: #{response.code}
    Response Message: #{response.message}
    Response Headers: #{response.to_hash.inspect}
    Response Body: #{response.body}
    ERROR
  end
  return users
end

def extract_active_users
  all_users = Nokogiri::XML(http_get(URL, OPTIONS))
  all_users.search('//user').each do |user|
    active_user = user.xpath('activated')
    if active_user.text == 'true'
      puts user
    end
  end
end

extract_active_users