use_ssl = true上没有方法

时间:2012-11-20 08:12:57

标签: ruby-on-rails ruby ruby-on-rails-3

我正在尝试使用rails控制器发送post请求,但是当我无法让控制器接受use_ssl = true时,我遇到了问题。 这是我的代码

require "net/http"
require "uri"

params = {'requestId' => '1', 
  'merchantId' => 'merchantid'
  'version' => '1.0.6', 
  'locale' => 'en_US', 
  'externalUserId' => '1234', 
  'partnerId' => 'partnerid', 
  'emailAddress' => 'test@email.com', 
  'deviceDTO' => "{'id' => '123123213', 'deviceType' => 'samsung'}",
  'timestamp' => Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%z')
}

url = URI.parse('https://example.com')
http = Net::HTTP::Post.new(url.host)
http.set_form_data(params)
#http.use_ssl = true
#http.verify_mode = OpenSSL::SSL::VERIFY_PEER


res = Net::HTTP.start(url.hostname, url.port) do |h|
  h.request(http)
  h.use_ssl = true
  h.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

错误是:

NoMethodError: undefined method `use_ssl=' for #<Net::HTTP::Post POST>
NoMethodError: undefined method `verify_mode=' for #<Net::HTTP::Post POST>

UPD 现在错误

Errno::ECONNRESET: Connection reset by peer

1 个答案:

答案 0 :(得分:2)

use_sslNet::HTTP中的一种方法,您正在创建一个Net::HTTP::Post对象。

您可能想要

url = URI.parse('https://example.com/')
http = Net::HTTP.new(url.host, url.port)
if url.scheme == 'https'
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
end
http.post(url.path, params)