【问题标题】:Trying to send XML with CDATA using node soap client without success尝试使用节点肥皂客户端发送带有 CDATA 的 XML,但没有成功
【发布时间】:2019-11-04 18:03:33
【问题描述】:

我正在尝试将此 xml 构建为输出,以发送到服务器:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:agil="http://schemas.datacontract.org/2004/07/Agillitas.Servico.ContratosDeServico">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:Integrar>
         <tem:request>
            <agil:Cabecalho>
               <agil:Metodo>44</agil:Metodo> 
               <agil:PublicKey>PUBLIC_KEY</agil:PublicKey>
               <agil:Senha>PASSWORD</agil:Senha>
               <agil:Usuario>USER</agil:Usuario>
            </agil:Cabecalho>
            <agil:Corpo>
             <![CDATA[
                  <CartaoDisponivelTesouraria>
                      <Empresa>XXXX</Empresa>
                      <Produto>X</Produto>
                      <Moeda>XXX</Moeda>
                  </CartaoDisponivelTesouraria>
            ]]>
            </agil:Corpo>
         </tem:request>
      </tem:Integrar>
   </soapenv:Body>
</soapenv:Envelope>

我已经创建了一个类来做这件事......

const soap = require('soap')

module.exports = {
    async run_method_44() {
        // attempt 1
        var args = {
            $xml: "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"xmlns:agil="http://schemas.datacontract.org/2004/07/Agillitas.Servico.ContratosDeServico"><soapenv:Header/><soapenv:Body><tem:Integrar><tem:request><agil:Cabecalho><agil:Metodo>44</agil:Metodo><agil:PublicKey>PUBLIC_KEY</agil:PublicKey><agil:Senha>PASSWORD</agil:Senha><agil:Usuario>USER</agil:Usuario></agil:Cabecalho><agil:Corpo><![CDATA[<CartaoDisponivelTesouraria><Empresa>XXXX</Empresa><Produto>X</Produto><Moeda>XXX</Moeda></CartaoDisponivelTesouraria>]]></agil:Corpo></tem:request></tem:Integrar></soapenv:Body></soapenv:Envelope>"
        }

        // attempt 2
        var args2 = {
             _xml: {'soapenv:Envelope': {
                    attributes: {
                        'xmlns:soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',
                        'xmlns:tem': 'http://tempuri.org/',
                        'xmlns:agil': 'http://schemas.datacontract.org/2004/07/Agillitas.Servico.ContratosDeServico',
                    },
                    'soapenv:Body': {
                        'tem:Integrar': {
                            'tem:request': {
                                'agil:Cabecalho': {
                                    'agil:Metodo': 44,
                                    'agil:PublicKey': "PUBLIC_KEY",
                                    'agil:Senha': "PASSWORD",
                                    'agil:Usuario': "USER"
                                },
                                'agil:Corpo': '<![CDATA[<CartaoDisponivelTesouraria><Empresa>XXXX</Empresa><Produto>X</Produto><Moeda>XXX</Moeda></CartaoDisponivelTesouraria>]]>'
                            }
                        }
                    }
                }}
        }

        // client
        client.Integrar(args, function(err, result, rawResponse, soapHeader, rawRequest) {
                console.log(result)
                console.log(rawResponse)
                console.log(soapHeader)
                console.log(rawRequest)
                console.log(err)
            }, {postProcess: function(_xml) {
                console.log(_xml)
                return _xml.replace('IAdminService_Integrar_InputMessage', 'Integrar')
            }})
    }
}

发送到服务器的最终 XML 与我所期望的完全不同......

谁能帮我弄清楚如何防止在最终请求时创建一些奇怪的标签?

那么问题来了:

我输出的 XML 完全不同然后我期望... 我尝试了不同的解决方案,但似乎没有任何效果......有人遇到过类似的情况来提示如何解决这个问题吗?

【问题讨论】:

    标签: node.js xml soap soap-client


    【解决方案1】:

    我在这里使用 xmlbuilder 来创建 xml 字符串

    const builder = require('xmlbuilder')
    
    const obj = {
        "soapenv:Envelope": {
          "@xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
          "@xmlns:tem": "http://tempuri.org/",
          "@xmlns:agil": "http://schemas.datacontract.org/2004/07/Agillitas.Servico.ContratosDeServico",
          "soapenv:Header": "",
          "soapenv:Body": {
            "tem:Integrar": {
              "tem:request": {
                "agil:Cabecalho": {
                  "agil:Metodo": "44",
                  "agil:PublicKey": "PUBLIC_KEY",
                  "agil:Senha": "PASSWORD",
                  "agil:Usuario": "USER"
                },
                "agil:Corpo": {
                    '#cdata': "\n             \n                  <CartaoDisponivelTesouraria>\n                      <Empresa>XXXX</Empresa>\n                      <Produto>X</Produto>\n                      <Moeda>XXX</Moeda>\n                  </CartaoDisponivelTesouraria>\n            \n            "
                }
              }
            }
          }
        }
      }
    
    const xml = builder.create(obj).end({pretty: true})
    console.log(xml);
    

    输出

    <?xml version="1.0"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:agil="http://schemas.datacontract.org/2004/07/Agillitas.Servico.ContratosDeServico">
      <soapenv:Header/>
      <soapenv:Body>
        <tem:Integrar>
          <tem:request>
            <agil:Cabecalho>
              <agil:Metodo>44</agil:Metodo>
              <agil:PublicKey>PUBLIC_KEY</agil:PublicKey>
              <agil:Senha>PASSWORD</agil:Senha>
              <agil:Usuario>USER</agil:Usuario>
            </agil:Cabecalho>
            <agil:Corpo>
              <![CDATA[
    
                      <CartaoDisponivelTesouraria>
                          <Empresa>XXXX</Empresa>
                          <Produto>X</Produto>
                          <Moeda>XXX</Moeda>
                      </CartaoDisponivelTesouraria>
    
                ]]>
            </agil:Corpo>
          </tem:request>
        </tem:Integrar>
      </soapenv:Body>
    </soapenv:Envelope>
    

    【讨论】:

    • 嘿!谢了,兄弟们!我在这里测试过...正在完美构建...您使用什么作为肥皂客户端?我需要将此发送给客户,但目前,当我发送时,它正在将 转换为 < >
    • 我通常只使用 http 客户端 - request 通常就足够了。
    • 我尝试使用request,但没有成功...我在服务器端遇到绑定错误...我现在正在尝试查看另一个soap客户端发送原始json请求.. . 无论如何谢谢
    • 您可以使用任何soap客户端,尝试使用代理捕获请求,然后转换为请求库。这样就容易多了:)
    • 我刚刚意识到如何执行请求...我以正确的方式发送请求...但返回不是 json,所以现在我需要知道如何将原始 XML 解析为json 对象...但会先尝试 google :) 刚刚取得进展
    猜你喜欢
    • 2017-09-27
    • 2013-11-20
    • 1970-01-01
    • 2016-10-22
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    相关资源
    最近更新 更多