简体   繁体   English

如何在Ruby中创建SOAP信封?

[英]How do I create a SOAP envelope in Ruby?

I am using Savon to build the client, but want to kno how to create the actual envelope. 我正在使用Savon来构建客户端,但想知道如何创建实际的信封。

I am thinking of using Nokogiri XML Builder and just pass in the values, but not sure if that's the right way. 我正在考虑使用Nokogiri XML Builder并传递值,但不确定这是否正确。

The SOAP envelope specs can be found here: SOAP信封规范可以在这里找到:

http://api.postalmethods.com/PostalWS.asmx?op=UploadFile http://api.postalmethods.com/PostalWS.asmx?op=UploadFile

My answer would be: Don't. 我的回答是:不要。

Savon will automatically create an envelope for you. Savon将自动为您创建一个信封。 You can pass whatever header and body you want, as arrays and hashes or XML strings. 您可以传递任何所需的标题和正文,如数组和散列或XML字符串。 You can manipulate existing (default) namespaces and even override input actions. 您可以操作现有(默认)命名空间甚至覆盖输入操作。 I'm not sure why anyone would need to construct a SOAP envelope in an XML parser such as nokogiri. 我不确定为什么有人需要在诸如nokogiri之类的XML解析器中构造SOAP信封。

If your reason for not wanting to use Savon to construct the envelope is that it lacks some sort of functionality that the endpoint requires, let me know what the exact problem is. 如果您不想使用Savon构建信封的原因是它缺少端点所需的某种功能,请告诉我确切的问题是什么。 I've had to make a fairly significant amount of changes to Savon in the past to satisfy certain APIs, so chances are I already have a patch for you. 我不得不在过去对Savon做出相当大的改动以满足某些API,所以很有可能我已经为你准备了一个补丁。 And if not, I can certainly point you in the right direction in order for you to create your own patch. 如果没有,我当然可以指出你正确的方向,以便你创建自己的补丁。

And if you still need to construct an envelope outside of Savon, I'd recommend you not use Savon at all and simply send the XML through one of the many great http libs out there. 如果你仍然需要在Savon之外构建一个信封,我建议你根本不使用Savon,只需通过其中一个很好的http库发送XML。

EDIT: 编辑:

To illustrate how to use SOAP headers (and namespaces), you can include them as well as body in the request method: 为了说明如何使用SOAP标头(和名称空间),您可以在请求方法中包含它们以及body:

client = Savon::Client.new "http://url.to/wsdl"

response = client.get do |soap|
  # Use merge! for namespaces so that you don't overwrite all of the original namespaces (while still retaining the ability to overwrite individual ones)
  soap.namespaces.merge!({
    "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
    "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema"
  })

  # Use soap.header just like you would soap.body
  soap.header = {
    # SOAP header hash
  }

  # soap.body as usual
  soap.body = {
    # SOAP body hash
  }
end

Savon treats inputs to SOAP services as Nested Hashes. Savon将SOAP服务的输入视为嵌套哈希。 The envelope is built by Savon so you generally shouldn't need to worry about it too much, if at all. 信封是由萨翁建造的,所以你通常不需要太担心它,如果有的话。

Have you actually managed to connect to the service yet and if not what errors are you getting? 你有没有真正设法连接到服务,如果不是你有什么错误?

If you have not yet connected and/or are having some problems with the concept of how to do this the following should get you started. 如果您还没有连接和/或在如何执行此操作的概念方面遇到一些问题,以下内容应该可以帮助您入门。

require 'savon'

client = Savon::Client.new http://api.postalmethods.com/PostalWS.asmx?wsdl
response = client.UploadFile do |soap|
  soap.body = {
    "Username" => "My User",
    "Password" => "My password"
    ...
  }
end

If you fill in the blanks at ... this should get you started although parsing the wsdl may not be the way to go forward in production. 如果你填写空白...这应该让你开始,虽然解析wsdl可能不是在生产中前进的方式。 Have a look at the Savon documentation for specific issues that you get. 查看Savon文档,了解您获得的具体问题。

Edit 编辑

If you are not sure about Savon and SOAP then you could just use the postalmethods gem as described here , rather than re-inventing the wheel 如果你不确定Savon和SOAP那么你可以使用这里描述的postalmethods gem ,而不是重新发明轮子

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

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