ruby - 在 Ruby 中面对 Net::HTTP Get 的问题

标签 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 代码,而不是预期的响应。我在这里做错了什么?

最佳答案

这来自 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

关于ruby - 在 Ruby 中面对 Net::HTTP Get 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26087817/

相关文章:

用于 GET、POST、PUT、DELETE 的 Net::HTTP 的 Ruby 示例

svg - 将参数传递给 HandlerFunc

ruby jiraSOAP net-http ssl 验证

ruby - 在 Hanami 模型中加入查询

ruby - 如何使用 Ruby Gtk 进行图像缩放和拖动选择?

c - 在 C 语言中扩展 Ruby - 将参数转换为 C 类型

mysql - 主动记录查询以从三个关联模型中获取数据

ruby-on-rails - x-www-form-urlencoded POST 请求和 net/http 上缺少查询参数

ruby-on-rails - 无法打开与本地主机 :3101 - Rails/Docker 的 TCP 连接

html - Material Design Lite 抽屉在导航链接点击时破坏了网站