You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Node.js的Strong-soap响应中添加SOAP Header

在strong-soap中为SOAP响应添加Header的实现方法

在strong-soap的异步服务方法中,你可以通过回调函数的第二个参数传递响应Header,官方基础示例仅展示了响应体传递,容易忽略这个参数的作用。

修改后的代码示例

var myService = {
  MyService: {
    MyPort: {
      MyAsyncFunction: function(args, callback, headers) {
        // 处理业务逻辑(比如调用目标REST接口)
        // ...

        // 构造符合WSDL定义的响应Header结构
        const responseHeaders = {
          // 替换为你的实际响应Header格式,示例如下:
          ResponseHeader: {
            RequestId: "req-789012",
            Status: "Processed"
          }
        };

        // 构造响应体
        const responseBody = {
          name: args.name
        };

        // 回调时同时传入响应体和响应Header
        callback(responseBody, responseHeaders);
      }
    }
  }
};

关键注意事项

  • 响应Header的结构必须严格匹配你WSDL中定义的响应Header格式,否则客户端会解析失败
  • 如果需要返回SOAP错误(Fault),可以使用回调的第三个参数:
    callback(null, null, {
      Fault: {
        Code: { Value: "soap:Sender", Subcode: { Value: "InvalidParam" } },
        Reason: { Text: "Name parameter cannot be empty" }
      }
    });
    
  • 入站请求的Header可通过方法第三个参数headers获取,你可以基于它生成对应响应Header(比如透传请求ID)

内容的提问来源于stack exchange,提问作者Jonnathan Sandino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 05:14:57