在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
答案 0 :(得分:1)
Ruby附带了Net :: HTTP库来发出HTTP请求。有许多宝石包装HTTP,我最喜欢的是Faraday。查看这些库,如何使用它们发出POST请求非常简单。
答案 1 :(得分:1)
有很多宝石是免费的,但如果您只想使用标准库,请尝试以下方法:
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)
答案 2 :(得分:1)
尝试: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