在ruby中发送带有身份验证的https发布请求

时间:2015-03-09 09:42:31

标签: ruby curl

我在这里卷曲了一下:

curl https://test-api.pin.net.au/1/charges -u your-secret-api-key: \ 
 -d "amount=400" \ -d "currency=USD" \

如何使用net/http发布此请求?它包括ssl和身份验证。

2 个答案:

答案 0 :(得分:0)

HTTP#post_form将直接执行

require 'net/http'
require 'uri'

url = URI.parse('https://test-api.pin.net.au/1/charges')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'user', 'pass'
req.use_ssl = true
req.form_data({'your-secret-api-key' => 'val1', 'amount' => 'val2', 'currency' => "USD"})

resp = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
puts resp

答案 1 :(得分:0)

我现在解决了这个问题。这是答案

uri = URI.parse('https://test-api.pin.net.au/1/charges')  
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http  
req = Net::HTTP::Post.new(uri.path)  
req.basic_auth 'user', 'pass'  
req.set_form_data({ 'amount' => 'val2', 'currency' => "USD"}  
http.request(req))  
end