面对Net :: Net Get在Ruby中的问题

时间:2014-09-28 17:55:22

标签: ruby net-http

这是我要在ruby中转换的curl请求:

curl -k --get --data 'session.id=48d59b37-5875-4e49-8d79-6cf097d740f&ajax=executeFlow&project=test&flow=two' https://localhost:8443/executor

我应该这样回答:

{
  "message" : "Execution submitted successfully with exec id 688",
  "project" : "test",
  "flow" : "two",
  "execid" : 688
}

我将它转换为以下ruby代码:

require 'json'
require 'uri'
require 'net/http'

url = URI("https://localhost:8443/executor")
http = Net::HTTP.new(url.host, url.port)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = (url.scheme == 'https')
request = Net::HTTP::Get.new(url.request_uri)
request.set_form_data({'session.id' => '48d59b37-5875-4e49-8d79-6cf097d740f', 'ajax' => 'executeFlow', 'project' => 'test', 'flow' => 'two'}) 
response = http.request(request)
puts response

但这会引发错误页面的HTML代码,而不是预期的响应。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

这来自Net::HTTP文档,我没有测试过。

url = URI("https://localhost:8443/executor")
request = Net::HTTP::Post.new(url)
request.set_form_data({'session.id' => '48d59b37-5875-4e49-8d79-6cf097d740f', 'ajax' => 'executeFlow', 'project' => 'test', 'flow' => 'two'}) 

response = Net::HTTP.start(url.hostname, url.port, use_ssl: url.scheme == 'https', verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
  http.request(request)
end