【问题标题】:handling xml responses from httpclient in javascript policy在 javascript 策略中处理来自 httpclient 的 xml 响应
【发布时间】:2014-08-29 12:16:17
【问题描述】:

我有一个后端 SOAP Web 服务,它有两种方法 - getCustomerByPhoneNumber 和 getOrderByCustomerId。我需要构建一个 apigee json 端点来获取客户的最新订单,该订单应以电话号码作为输入。

这意味着,我必须创建一个 API 端点和一个指向 SOAP 方法 getOrderByCustomerId 的资源。构建一个内部调用代理(最好是 javascript 策略并使用 httpClient 代理对象)来调用 SOAP 方法 getCustomerByPhoneNumber 以获取客户 ID 并将 customerId 传递给端点方法 getOrderByCustomerId。 我关心的是 - 如何处理来自 getCustomerByPhoneNumber 方法的 SOAP 响应!?如果是 JSON,那么很容易编写逻辑来从 javascript 代码中读取值。

如何在 javascript 中轻松解析 xml/soap 响应或在 javascript 中将 xml 转换为 JSON 以像对象一样读取响应?

或者,作为另一种选择,我是否应该将所有 SOAP 方法转换为 REST 方法,并且包装器累积方法应该调用转换后的 REST 方法(在调用代理中)而不是直接调用 SOAP 方法。

有什么见解吗?

【问题讨论】:

    标签: apigee


    【解决方案1】:

    Apigee 使用 E4X,它增加了对 XML 的支持(请参阅:http://apigee.com/docs/api-services/content/javascript-object-model)。下面的示例演示了如何使用 SOAP 响应。这是我使用来自w3schools 的示例肥皂消息快速整理的。

    示例 SOAP 响应消息:

    <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
      <soap:Body xmlns:m="http://www.example.org/stock">
        <m:m:GetStockPriceResponse>
          <m:Price>34.5</m:Price>
        </m:m:GetStockPriceResponse>
      </soap:Body>
    </soap:Envelope>
    

    Apigee 中的示例 javascript:

    var mockResponse ='<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"><soap:Body xmlns:m="http://www.example.org/stock"><m:GetStockPriceResponse><m:Price>34.5</m:Price></m:GetStockPriceResponse></soap:Body></soap:Envelope>';
    
    var soap = new Namespace('http://www.w3.org/2001/12/soap-envelope');
    var m = new Namespace('http://www.example.org/stock');
    var xmlObj = new XML (mockResponse);
    
    var stockPrice = xmlObj.soap::Body.m::GetStockPriceResponse.m::Price;
    
    
    response.content.status = 200;
    response.content.status.message = 'OK';
    response.headers['content-type'] = 'text/plain';
    response.content = 'stockPrice: ' + stockPrice;
    

    给您留下如下回复:

    HTTP/1.1 200 OK
    content-type: text/plain
    Content-Length: 16
    
    stockPrice: 34.5
    

    【讨论】:

      猜你喜欢
      • 2016-04-01
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      相关资源
      最近更新 更多