Submitting a POST request from a rails controller to another site [duplicate]

限于喜欢 提交于 2019-12-12 02:43:20

问题


There is an exact duplicate of this question that is over four years old here

Given that it's been so long, my question now is, is that answer still accurate? Is there a better way to do this now?


回答1:


You do something like this. Write a function in your controller like this:

require 'net/http'
require 'net/https'
class custom_class
def get_api_call(args_hash)
    uri = URI.parse("sample_api_url")
    uri.query = URI.encode_www_form(what_args_you_want_to_send)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    request = Net::HTTP::Post.new(uri.request_uri)
    http.request(request).body
end

private

def what_args_you_want_to_send
{
      "varname1" => var1,
      "varname2" => var2,
      "varname3" => var3,
      "varname4" => var4
}
end

The result of that function will have the answer from the server you are send a request to



来源:https://stackoverflow.com/questions/19289223/submitting-a-post-request-from-a-rails-controller-to-another-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!