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

在WSO2中配置Content-Type为application/json-patch+json失败求助

问题:WSO2发送JSON Patch请求时Content-Type不生效

需要向外部API发送Content-Type为application/json-patch+json的PATCH请求,尝试设置传输头属性后,实际发送的Content-Type仍是application/json,而非预期值。

已做配置

  • 在序列中添加传输头:
<header name="Content-Type" scope="transport" value="application/json-patch+json"/>
  • 在axis2.xml中配置消息构建器:
<messageBuilder contentType="application/json-patch+json"  
                class="org.wso2.micro.integrator.core.json.JsonStreamBuilder"/>

代理配置详情

<?xml version="1.0" encoding="UTF-8"?>
<proxy name="TestProxy" startOnLoad="true" transports="http https vfs" xmlns="http://ws.apache.org/ns/synapse">
    <target>
        <inSequence> 
            <clone sequential="true">
                 <target>
                    <sequence>
                        <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
                        <payloadFactory media-type="json">
                            <format>
                                [
                                    {
                                        "op":"add",
                                        "path":"/example/path",
                                        "value":{ 
                                            "attributes": "test-attribute"
                                            }
                                        
                                        ]
                            </format>
                            <args/>
                        </payloadFactory>
                        
                        <property name="ContentType" value="application/json-patch+json" scope="axis2"/>
                        <property name="Content-Type" value="application/json-patch+json" scope="transport"/>
                        
                        <log>
                            <property name="test-payload" expression="json-eval($)"/>
                        </log>
                        
                        <property expression="fn:concat('Bearer ','**xyz**')" name="Authorization" scope="transport" type="STRING"/>
                        <call>
                            <endpoint>
                                <http method="patch" uri-template="https://api/v1/test">
                                    <suspendOnFailure>
                                        <initialDuration>-1</initialDuration>
                                        <progressionFactor>-1</progressionFactor>
                                        <maximumDuration>0</maximumDuration>
                                    </suspendOnFailure>
                                    <markForSuspension>
                                        <retriesBeforeSuspension>0</retriesBeforeSuspension>
                                    </markForSuspension>
                                </http>
                            </endpoint>
                        </call>
                        <log>
                            <property expression="json-eval($)" name="patch-response"/>
                        </log>
                    </sequence>
                </target> 
                <target>
                    <!-- some other logic -->
                </target>
            </clone>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </target>
</proxy>

请求日志片段

[2023-03-28 10:46:18,155] DEBUG {headers} - http-outgoing-1 >> PATCH /api/v1/test HTTP/1.1
[2023-03-28 10:46:18,155] DEBUG {headers} - http-outgoing-1 >> Authorization: Bearer **xyz**
[2023-03-28 10:46:18,155] DEBUG {headers} - http-outgoing-1 >> activityid: f6fabf50-e480-4f71-aacb-c8b8abe3e376
[2023-03-28 10:46:18,155] DEBUG {headers} - http-outgoing-1 >> Accept: */*
[2023-03-28 10:46:18,160] DEBUG {headers} - http-outgoing-1 >> contentType: application/json-patch+json
[2023-03-28 10:46:18,160] DEBUG {headers} - http-outgoing-1 >> Content-Type: application/json
[2023-03-28 10:46:18,160] DEBUG {headers} - http-outgoing-1 >> SOAPAction:
[2023-03-28 10:46:18,160] DEBUG {headers} - http-outgoing-1 >> Transfer-Encoding: chunked
[2023-03-28 10:46:18,163] DEBUG {headers} - http-outgoing-1 >> Connection: Keep-Alive
[2023-03-28 10:46:18,163] DEBUG {headers} - http-outgoing-1 >> User-Agent: Synapse-PT-HttpComponents-NIO
[2023-03-28 10:46:18,192] DEBUG {wire} - HTTPS-Sender I/O dispatcher-1 << "PATCH /api/v1/test HTTP/1.1[\r][
]"
[2023-03-28 10:46:18,192] DEBUG {wire} - HTTPS-Sender I/O dispatcher-1 << "Authorization: Bearer **xyz**[\r][
]"
[2023-03-28 10:46:18,192] DEBUG {wire} - HTTPS-Sender I/O dispatcher-1 << "activityid: f6fabf50-e480-4f71-aacb-c8b8abe3e376[\r][
]"
[2023-03-28 10:46:18,195] DEBUG {wire} - HTTPS-Sender I/O dispatcher-1 << "Accept: */*[\r][
]"
[2023-03-28 10:46:18,197] DEBUG {wire} - HTTPS-Sender I/O dispatcher-1 << "contentType: application/json-patch+json[\r][
]"
[2023-03-28 10:46:18,198] DEBUG {wire} - HTTPS-Sender I/O dispatcher-1 << "Content-Type: application/json[\r][
]"
[2023-03-28 10:46:18,198] DEBUG {wire} - HTTPS-Sender I/O dispatcher-1 << "SOAPAction: [\r][
]"
[2023-03-28 10:46:18,200] DEBUG {wire} - HTTPS-Sender I/O dispatcher-1 << "Transfer-Encoding: chunked[\r][
]"
[2023-03-28 10:46:18,200] DEBUG {wire} - HTTPS-Sender I/O dispatcher-1 << "Connection: Keep-Alive[\r][
]"
[2023-03-28 10:46:18,201] DEBUG {wire} - HTTPS-Sender I/O dispatcher-1 << "User-Agent: Synapse-PT-HttpComponents-NIO[\r][
]"
[2023-03-28 10:46:18,201] DEBUG {wire} - HTTPS-Sender I/O dispatcher-1 << "[\r][
]"
[2023-03-28 10:46:18,203] DEBUG {wire} - HTTPS-Sender I/O dispatcher-1 << "236[\r][
]"
[2023-03-28 10:46:18,203] DEBUG {wire} - HTTPS-Sender I/O dispatcher-1 << "[{\"op\":\"add\",\"path\":\"/example/path\",\"value\":{\"attributes\":{\"test-attribute\"}}}][\r][
]"
[2023-03-28 10:46:18,203] DEBUG {wire} - HTTPS-Sender I/O dispatcher-1 << "0[\r][
]"
[2023-03-28 10:46:18,203] DEBUG {wire} - HTTPS-Sender I/O dispatcher-1 << "[\r][
]"

解决方案

从日志可见,请求中同时存在contentType和Content-Type头,其中Content-Type: application/json是由messageType属性覆盖导致的,需按以下步骤修改:

  1. 修正messageType属性
    原配置中<property name="messageType" scope="axis2" type="STRING" value="application/json"/>会强制设置消息Content-Type,覆盖后续配置。将其改为:
<property name="messageType" scope="axis2" type="STRING" value="application/json-patch+json"/>

或直接删除该属性。

  1. 统一Content-Type配置
    确保axis2范围的ContentType和transport范围的Content-Type保持一致,且在payloadFactory之后设置,可保留现有两个属性,避免冲突。

  2. 添加消息格式化器
    在axis2.xml中补充配置对应消息格式化器,确保请求处理正确:

<messageFormatter contentType="application/json-patch+json"  
                    class="org.wso2.micro.integrator.core.json.JsonStreamFormatter"/>

修改后的序列关键片段:

<sequence>
    <property name="messageType" scope="axis2" type="STRING" value="application/json-patch+json"/>
    <payloadFactory media-type="json">
        <format>
            [
                {
                    "op":"add",
                    "path":"/example/path",
                    "value":{ 
                        "attributes": "test-attribute"
                        }
                    
                    ]
        </format>
        <args/>
    </payloadFactory>
    
    <property name="Content-Type" value="application/json-patch+json" scope="transport"/>
    
    <!-- 后续日志和调用逻辑不变 -->
</sequence>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 21:48:07