2017-07-20 74 views
0

新到Rails 5.1,並試圖發送淨/ HTTP和URI的PUT請求,不知道如果我正確設置參數的Rails 5.1 - 網/ HTTP PUT請求

需要把[「授權」]在頁眉

def put_function 
    uri = URI.parse('https://api.url.com/venues/' + ENV['VENUE_ID'] + '/request') 
    params = { 'email'  => request.email, 
      'notes'  => request.comments } 

    req = Net::HTTP::Put.new(uri) 
    req.body = params 
    req.authorization = ENV['AUTH_CODE'] 

    p req 

    data = JSON.parse(req.body) 
    p data 
end 

回答

0
require 'uri' 
require 'net/http' 

url = URI("https://api.url.com/venues/" + ENV['VENUE_ID'] + "/[email protected]&notes=test") 

http = Net::HTTP.new(url.host, url.port) 
http.use_ssl = true 
http.verify_mode = OpenSSL::SSL::VERIFY_NONE 

request = Net::HTTP::Put.new(url) 
request["authorization"] = ENV['AUTH_CODE'] 
request["cache-control"] = 'no-cache' 

response = http.request(request) 
puts response.read_body 
+0

除非你確信SSL配置錯誤,'http.verify_mode = OpenSSL的:: SSL :: VERIFY_NONE'是,一般,不是一個偉大的計劃。 –