简体   繁体   English

使用Savon(Ruby on Rails)访问SOAP请求的请求主体

[英]Accessing the request body of a SOAP request with Savon (Ruby on Rails)

I'm using Savon gem in Ruby on Rails to communicate with a wsdl WS. 我在Ruby on Rails中使用Savon gem与wsdl WS进行通信。 Everything is working fine, but I want to log the request XML using a custom log, ie not Rails or Savon logger. 一切正常,但我想使用自定义日志记录请求XML,即不是Rails或Savon记录器。 My code looks something like this: 我的代码看起来像这样:

    response = self.client.request :add_order do
      soap.body = { 
        :indata => {
          "CustomerNo" => config[:kundnr],
          "Pwd" => config[:password],
          "OrderDate" => order["purchase_order_date"].strftime('%Y%m%d')
        }
      }
    end

Accessing the response is no problem, but what about the request? 访问响应没有问题,但请求怎么办? I need to be able to see what has been sent in my production environment by logging the XML to a DB-field. 我需要通过将XML记录到DB字段来查看生产环境中发送的内容。

Right now there's no easy way to get the request XML. 现在,没有简单的方法来获取请求XML。 But as you noticed, Savon logs each request and response to a specified logger. 但是正如您所注意到的, Savon会将每个请求和响应记录到指定的记录器中。 So if you're not changing the log level, you could use a custom Logger-like object that responds to :debug and store what gets logged. 因此,如果您不更改日志级别,则可以使用响应的自定义Logger对象:debug并存储记录的内容。

module DBLogger
  def self.debug(message)
    p message  # replace with custom impl.
  end
end

Savon.configure do |config|
  config.logger = DBLogger
end

Hope that helps! 希望有所帮助!

This wasn't explicitly stated in the official answer, but I had to include a couple more options to get requests/responses to log properly. 这在官方答复中没有明确说明,但我必须包含更多选项来获取正确记录的请求/响应。 In my case, I just used Rails.logger instead of rolling my own: 在我的例子中,我只使用Rails.logger而不是自己滚动:

client = Savon.client(
  wsdl: 'http://example.com?wsdl',
  log: true,
  logger: Rails.logger,
  log_level: :debug
)

client.call(:example_operation, message: {example: 'example'})

The log_level might default to debug, but there's no harm in being explicit :) But without the log: true option, you won't see the actual request/response bodies, only urls and statuses. log_level可能默认为debug,但显式没有任何害处:)但是如果没有log: true选项,您将看不到实际的请求/响应主体,只会看到网址和状态。

I'm using Savon 2.7.2 for reference. 我正在使用Savon 2.7.2作为参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM