将 POST 数据从控制器提交到 Rails 中的另一个网站
用户提交包含一些基本数据的表单。
数据由控制器中的操作接收和处理,并且添加了需要保持私密的更多信息。
数据由控制器中的操作接收和处理,然后我需要向外部网站发送一个帖子请求,其中包含来自控制器的所有组合数据。
做这个的最好方式是什么?
User submits a form with some basic data.
The data is received and treated by an action in the controller and more information that needs to remain private is added.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最简单的方法是使用 ruby 核心库:
专业提示:使用诸如 delayed_job< 之类的 gem 执行异步请求/a> 或背景_rb
The simpliest way is using ruby core library:
Pro Tip: Do an asynchronous request, using a gem like delayed_job or background_rb
抱歉,我忘记提及我正在连接到安全服务器。 这似乎是我收到文件结尾错误的原因。 添加 using 'net/https' 并在连接上调用 use_ssl 解决了问题。 感谢大家的帮助。
这是基于 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.
This is based off the source for the post_form method, so i guess I'll give vlad.zloteanu the answer.
如果外部服务器是 RESTful,则只需创建一个 ActiveResource 模型处理您的数据。
If the external server is RESTful, then simply create an ActiveResource model to handle your data.
我不认为redirect_to处理post请求,因为它使用http 302(?),它只是获取另一个页面。
我相信你可以做这样的事情
:这是空中编码的,尚未经过尝试或测试
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
note: this is air coded and has not been tried or tested
如果你想发送 JSON,你只需要如下所示(在 Rails 6 中测试):
If you want to send JSON, all you need is something like the following (tested in Rails 6):