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

WSO2 Enterprise Integrator 6.5.0如何处理返回PDF的API响应?

在WSO2序列中处理API返回的PDF响应方案

问题分析

API返回的是包含PDF二进制内容的JSON结构({"binary": "binarycontent"}),直接回环会导致响应为空且无下载选项,因为WSO2默认按JSON处理响应,未正确解析并转换二进制内容。

处理步骤及序列配置

需通过序列完成提取二进制字段→解码→设置响应头→替换消息体的流程,具体配置如下:

<sequence name="ProcessPDFResponse" xmlns="http://ws.apache.org/ns/synapse">
    <!-- 1. 从JSON响应中提取base64编码的PDF二进制内容 -->
    <property name="pdfBase64Content" expression="json-eval($.binary)" scope="default" type="STRING"/>
    
    <!-- 2. 解码base64内容为二进制数据 -->
    <base64Decode>
        <source>pdfBase64Content</source>
        <target>pdfBinaryContent</target>
        <sourceType>STRING</sourceType>
        <targetType>BINARY</targetType>
    </base64Decode>
    
    <!-- 3. 设置响应头,指定内容类型和下载属性 -->
    <property name="HTTP_SC" value="200" scope="axis2"/>
    <property name="Content-Type" value="application/pdf" scope="axis2"/>
    <property name="Content-Disposition" value="attachment; filename=document.pdf" scope="axis2"/>
    
    <!-- 4. 将解码后的二进制数据设置为消息体 -->
    <payloadFactory media-type="xml">
        <format><binary>$1</binary></format>
        <args>
            <arg evaluator="xml" expression="$ctx:pdfBinaryContent"/>
        </args>
    </payloadFactory>
    
    <!-- 5. 清除原有JSON相关配置,确保使用PDF格式器 -->
    <property name="messageType" value="application/pdf" scope="axis2"/>
</sequence>

关键说明

  • json-eval($.binary):精准提取JSON结构中binary字段的base64内容
  • base64Decode:将字符串形式的base64编码转换为二进制流,这是生成有效PDF的核心步骤
  • Content-Disposition头:强制浏览器触发文件下载行为,指定文件名
  • 最后设置messageType为application/pdf:确保WSO2使用你已配置的ExpandingMessageFormatter处理响应

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 14:23:21