如何在Synapse API的Out Sequence中回传短信服务商的文本响应?
问题分析与解决方案
场景
向API发送请求载荷后,API会在In Sequence中调用短信服务商,需要原样回传短信服务商的响应。目前API运行正常,手机可接收短信,但无法在Out Sequence中回传服务商的响应。短信服务商的响应体为文本格式,响应头信息见附图。
API代码
<?xml version="1.0" encoding="UTF-8"?> <api context="/mobilink" name="MobilinkSmsApi" xmlns="http://ws.apache.org/ns/synapse"> <resource methods="POST" uri-template="/send"> <inSequence> <property action="remove" name="TRANSPORT_HEADERS" scope="axis2"/> <property description="username" expression="json-eval($.username)" name="uri.var.username" scope="default" type="STRING"/> <property description="password" expression="json-eval($.password)" name="uri.var.password" scope="default" type="STRING"/> <property description="to" expression="json-eval($.to)" name="uri.var.to" scope="default" type="STRING"/> <property description="from" expression="json-eval($.from)" name="uri.var.from" scope="default" type="STRING"/> <property description="message" expression="json-eval($.message)" name="uri.var.message" scope="default" type="STRING"/> <log level="full"/> <call> <endpoint> <http method="post" statistics="enable" trace="enable" uri-template="https://coXXXXX.XXXX.com/sendsms_url.html?Username={uri.var.username}&Password={uri.var.password}&From={uri.var.from}&To={uri.var.to}&Message={uri.var.message}"> <suspendOnFailure> <initialDuration>-1</initialDuration> <progressionFactor>-1</progressionFactor> <maximumDuration>0</maximumDuration> </suspendOnFailure> <markForSuspension> <retriesBeforeSuspension>0</retriesBeforeSuspension> </markForSuspension> </http> </endpoint> </call> </inSequence> <outSequence> <log category="TRACE" level="full"/> <property description="Content-Type" name="Content-Type" scope="default" type="STRING" value="text/html"/> <property name="messageType" scope="axis2" type="STRING" value="text/html"/> <respond/> </outSequence> <faultSequence> <log category="ERROR" level="full"/> </faultSequence> </resource> </api>
问题
需要在Out Sequence中回传短信服务商的响应,尝试设置Content-Type和messageType为text/html但未生效,该如何解决?
解决方案
1. 调整Content-Type的作用域
你当前把Content-Type设置在default作用域,这不会影响传输头。需要将其移到axis2作用域,这样才能正确设置响应的Content-Type头:
<property name="Content-Type" scope="axis2" type="STRING" value="text/plain"/>
(注:如果服务商响应是纯文本,用text/plain比text/html更合适,可根据实际响应格式调整)
2. 保留服务商的响应头(可选)
如果需要原样回传服务商的所有响应头,可以在Out Sequence开头添加以下配置,复制服务商的响应头到当前消息:
<property action="copy" name="TRANSPORT_HEADERS" scope="axis2" expression="$trp:*"/>
3. 禁用自动消息格式化
如果WSO2自动将文本响应解析为XML或其他格式,可添加属性禁用格式化:
<property name="NO_ENTITY_BODY" scope="axis2" action="remove"/> <property name="messageType" scope="axis2" type="STRING" value="text/plain"/>
4. 修改后的完整Out Sequence
<outSequence> <log category="TRACE" level="full"/> <!-- 复制服务商响应头(可选) --> <property action="copy" name="TRANSPORT_HEADERS" scope="axis2" expression="$trp:*"/> <!-- 设置正确的Content-Type和消息类型 --> <property name="Content-Type" scope="axis2" type="STRING" value="text/plain"/> <property name="messageType" scope="axis2" type="STRING" value="text/plain"/> <respond/> </outSequence>
5. 验证响应传递
先检查In Sequence中的log输出,确认已经成功接收到短信服务商的响应内容。如果log中能看到响应,说明问题出在Out Sequence的格式设置;如果log中没有响应,需要检查Call Mediator的同步调用配置(WSO2默认同步Call会自动将响应传递到Out Sequence,但若有异步配置需要调整)。
内容的提问来源于stack exchange,提问作者Ayub Jamal
相关产品推荐
相关产品推荐

