Ruby:将数据发布到url

在rails应用程序中,我希望能够在不访问网站的情况下将某些信息发布到方法中的其他网站。 我对这个话题相当新,所以好像我已经5岁了。

我的想法到目前为止:

def create_button button = { :name => 'test', :type => 'buy_now', :callback_url => 'http://www.example.com/my_custom_button_callback', :description => 'sample description', :include_email => true } "https://thesite.com/api/v1" + button.to_query # post logic here end 

Ruby附带了Net :: HTTP库来发出HTTP请求。 有很多gem包装HTTP,我最喜欢的是法拉第 。 查看这些库,如何使用它们发出POST请求非常简单。

有很多gem可以免费使用,但如果您只想使用标准库,请尝试以下方法:

 require "net/http" require 'net/https' require "uri" uri = URI.parse("https://thesite.com/api/v1") https = Net::HTTP.new(uri.host,uri.port) https.use_ssl = true req = Net::HTTP::Post.new(uri.path) # I'm unsure if symbols will work button = { "name" => 'test', "type" => 'buy_now', "callback_url" => 'http://www.example.com/my_custom_button_callback', "description" => 'sample description', "include_email" => true } req.set_form_data(button) res = https.request(req) 

尝试: Net :: HTTP

 uri = URI.parse("https://thesite.com/api/v1") button = { :name => 'test', :type => 'buy_now', :callback_url => 'http://www.example.com/my_custom_button_callback', :description => 'sample description', :include_email => true } res = Net::HTTP.post_form(uri,button) res.code res.body # response from api