无法通过'Ruby'语言使用'Get'请求获取XML响应

时间:2013-07-01 11:10:15

标签: ruby xml get net-http

使用Ruby语言触发'GET'请求后,我无法得到xml响应。我的当前代码如下:

require 'net/https'
require 'uri'
require 'base64'

base_url = '<URL>'
app_guid = '<App GUID Value>'
format = "xml"

# Example using bug.fetch

params = {
  "appGUID" => app_guid,
  "format" => format,
  "method" => "bug.fetch",
  "bugId" => "1234567"
  }

puts "XML Response"

res = Net::HTTP.post_form(URI.parse(base_url), params)
puts res.body

1 个答案:

答案 0 :(得分:1)

正如Frederick所说,您的代码正在发出POST请求。如果您想使用GET执行:

uri = URI.parse(base_url)
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)

如果您仍然遇到错误,您可能希望使用以下替代语法:

uri = URI.parse(base_url)
uri.query = URI.encode_www_form(params)
conn = Net::HTTP.new(uri.host, uri.port)
conn.use_ssl = true
res = conn.get uri.request_uri