Ruby - 从传入的 http 调用中获取请求正文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2657704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:32:20  来源:igfitidea点击:

Ruby - Get request body from incoming http call

ruby-on-railsrubyjson

提问by opsb

I am receiving http requests to my rails application to a url /account/postback

我正在接收到我的 Rails 应用程序的 http 请求到一个 url /account/postback

The body of this incoming request contains some json that I need to retrieve, how can I do this in ruby?

此传入请求的正文包含一些我需要检索的 json,我如何在 ruby​​ 中执行此操作?

回答by opsb

The following should print the body of the request

以下应该打印请求的正文

routes.rb

路由文件

map.connect 'account/:action', :controller => 'accounts'

accounts_controller.rb

account_controller.rb

class AccountsController < ApplicationController
  def postback
    puts request.body.read    
  end
end

回答by fagiani

If your HTTP call is using the POST verb you could alternatively use request.raw_postto retrieve the contents sent in the request's body.

如果您的 HTTP 调用使用 POST 动词,您也可以request.raw_post用来检索请求正文中发送的内容。

Hope it helps!

希望能帮助到你!