我正在使用wso2ESB中的REST API调用另一个具有基本身份验证的服务。
这是我的API代码:
<resource methods="POST" uri-template="/deleteZoneFromEnrolment/">
<inSequence>
<property xmlns:ns="http://org.apache.synapse/xsd" name="Authorization" expression="fn:concat('Basic ', base64Encode('user:password'))" scope="transport"></property>
<property name="DISABLE_CHUNKING" value="true" scope="axis2"></property>
<payloadFactory media-type="xml">
<format>
<enr:DeleteZoneFromEnrolment xmlns:enr="http://someURL/Enrolment.xsd">
<Enrolment>$1</Enrolment>
<Zone>$2</Zone>
</enr:DeleteZoneFromEnrolment>
</format>
<args>
<arg evaluator="json" expression="$.enrolment"></arg>
<arg evaluator="json" expression="$.zone"></arg>
</args>
</payloadFactory>
<send>
<endpoint key="dev_enrolmentEndpoint"></endpoint>
</send>
<log level="full"></log>
</inSequence>
<outSequence>
<property name="messageType" value="application/json" scope="axis2" type="STRING"></property>
<send></send>
</outSequence>
<faultSequence>
<sequence key="esb_error_handling_response"></sequence>
</faultSequence>
我使用json POST调用这个REST API,如下所示:
{
'enrolment':'whatever',
'zone':'1'
}
如果我硬编码用户和密码,它很好用。但我要向前迈进一步。我需要使用post JSON来传递这个基本授权,我使用这个post JSON来调用ESB中的REST API,或者设置这个API来通过GET/post调用传递身份验证。
有人知道怎么做吗?这就是我正在尝试但没有成功的事情:
在我调用API(POST)之前,在我的应用程序中
http://esbUrl:8280/api/deleteZoneFromEnrolment
),我在Base64中编码字符串“user:password”,结果如下:“cHJhYmF0aDpwcmFiYXRo”。我这样更改我的API代码:
<resource methods="POST" uri-template="/deleteZoneFromEnrolment/">
<inSequence>
<property xmlns:ns="http://org.apache.synapse/xsd" name="Authorization" expression="fn:concat('Basic ', json-eval($.user))" scope="transport"></property>
<property name="DISABLE_CHUNKING" value="true" scope="axis2"></property>
<payloadFactory media-type="xml">
<format>
<enr:DeleteZoneFromEnrolment xmlns:enr="http://someURL/Enrolment.xsd">
<Enrolment>$1</Enrolment>
<Zone>$2</Zone>
</enr:DeleteZoneFromEnrolment>
</format>
<args>
<arg evaluator="json" expression="$.enrolment"></arg>
<arg evaluator="json" expression="$.zone"></arg>
<arg evaluator="json" expression="$.user"></arg>
</args>
</payloadFactory>
<send>
<endpoint key="dev_enrolmentEndpoint"></endpoint>
</send>
<log level="full"></log>
</inSequence>
<outSequence>
<property name="messageType" value="application/json" scope="axis2" type="STRING"></property>
<send></send>
</outSequence>
<faultSequence>
<sequence key="esb_error_handling_response"></sequence>
</faultSequence>
以及POST json:
{
'enrolment':'whatever',
'zone':'1',
'user':'cHJhYmF0aDpwcmFiYXRo'
}