将 POST 数据从控制器提交到 Rails 中的另一个网站

发布于 2024-07-29 14:12:21 字数 189 浏览 2 评论 0原文

  1. 用户提交包含一些基本数据的表单。

  2. 数据由控制器中的操作接收和处理,并且添加了需要保持私密的更多信息。

    数据由控制器中的操作接收和处理,
  3. 然后我需要向外部网站发送一个帖子请求,其中包含来自控制器的所有组合数据。

做这个的最好方式是什么?

  1. User submits a form with some basic data.

  2. The data is received and treated by an action in the controller and more information that needs to remain private is added.

  3. Then I need to send a post request to an external website with all of the combined data from the controller.

What is the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沒落の蓅哖 2024-08-05 14:12:21

最简单的方法是使用 ruby​​ 核心库:

require "uri"
require "net/http"

params = {'box1' => 'Nothing is less important than which fork you use. Etiquette is the science of living. It embraces everything. It is ethics. It is honor. -Emily Post',
'button1' => 'Submit'
}
x = Net::HTTP.post_form(URI.parse('http://www.interlacken.com/webdbdev/ch05/formpost.asp'), params)
puts x.body

专业提示:使用诸如 delayed_job< 之类的 gem 执行异步请求/a> 或背景_rb

The simpliest way is using ruby core library:

require "uri"
require "net/http"

params = {'box1' => 'Nothing is less important than which fork you use. Etiquette is the science of living. It embraces everything. It is ethics. It is honor. -Emily Post',
'button1' => 'Submit'
}
x = Net::HTTP.post_form(URI.parse('http://www.interlacken.com/webdbdev/ch05/formpost.asp'), params)
puts x.body

Pro Tip: Do an asynchronous request, using a gem like delayed_job or background_rb

染墨丶若流云 2024-08-05 14:12:21

抱歉,我忘记提及我正在连接到安全服务器。 这似乎是我收到文件结尾错误的原因。 添加 using 'net/https' 并在连接上调用 use_ssl 解决了问题。 感谢大家的帮助。

require 'net/https'
require 'open-uri'

url = URI.parse('https://MY_URL')
req = Net::HTTP::Post.new(url.path)
req.form_data = data
req.basic_auth url.user, url.password if url.user
con = Net::HTTP.new(url.host, url.port)
con.use_ssl = true
con.start {|http| http.request(req)}    

这是基于 post_form 方法的源代码,所以我想我会给 vlad.zloteanu 答案。

Sorry, I neglected to mention that I was connecting to secure server. This seems to have been the reason that I was getting end of file errors. Adding using 'net/https' and calling use_ssl on connection solved the problem. Thanks for everyones help.

require 'net/https'
require 'open-uri'

url = URI.parse('https://MY_URL')
req = Net::HTTP::Post.new(url.path)
req.form_data = data
req.basic_auth url.user, url.password if url.user
con = Net::HTTP.new(url.host, url.port)
con.use_ssl = true
con.start {|http| http.request(req)}    

This is based off the source for the post_form method, so i guess I'll give vlad.zloteanu the answer.

终止放荡 2024-08-05 14:12:21

如果外部服务器是 RESTful,则只需创建一个 ActiveResource 模型处理您的数据。

If the external server is RESTful, then simply create an ActiveResource model to handle your data.

反话 2024-08-05 14:12:21

我不认为redirect_to处理post请求,因为它使用http 302(?),它只是获取另一个页面。

我相信你可以做这样的事情

Class MyController < ActionController
    require 'net/http'

    def my_method
        #do something with the data/model

        my_connection = Net::HTTP.new('www.target.com', 80)
        reponse = my_connection.post(path_within_url, data)

        #do something with response if you want
    end

end

:这是空中编码的,尚未经过尝试或测试

I don't think redirect_to handles post requests because it uses http 302 (?) which just GETs the other page.

I believe you can do something like this

Class MyController < ActionController
    require 'net/http'

    def my_method
        #do something with the data/model

        my_connection = Net::HTTP.new('www.target.com', 80)
        reponse = my_connection.post(path_within_url, data)

        #do something with response if you want
    end

end

note: this is air coded and has not been tried or tested

原谅我要高飞 2024-08-05 14:12:21

如果你想发送 JSON,你只需要如下所示(在 Rails 6 中测试):

Net::HTTP.post(
  URI('https://example.com/some/path'),
  { "this is the": "request body" }.to_json,
  'Content-Type' => 'application/json'
)

If you want to send JSON, all you need is something like the following (tested in Rails 6):

Net::HTTP.post(
  URI('https://example.com/some/path'),
  { "this is the": "request body" }.to_json,
  'Content-Type' => 'application/json'
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文