2012-11-20 59 views
0

我想發送使用rails控制器的post請求,但是我遇到了問題,當我不能讓控制器接受use_ssl = true。 這裏是我的代碼沒有方法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' => '[email protected]', 
    '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 
+0

是你的Rails應用程序安裝在SSL模式下運行? –

回答

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) 
+0

我該如何使用use_ssl發佈無Post對象的請求? 錯誤現在'Errno :: ECONNRESET:通過同級重置連接 –