I was trying to do a POC to encode a json payload to base64 format in WSO2. I have used payload factory to form a json payload which looks like below:
{
"employee_id": 1012967,
"employe_name": "Tarun",
"employee_dept": "Finance",
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5UZG1aak00WkRrM05qWTBZemM1TW1abU9EZ3dNVEUzTVdZd05ERTVNV1JsWkRnNE56YzRaQT09In0.eyJhdWQiOiJodHRwOlwvXC9vcmcud3NvMi5hcGltZ3RcL2dhdGV3YXkiLCJzdWIiOiJhZG1pbkBjYXJib24uc3VwZXIiLCJhcHBsaWNhdGlvbiI6eyJvd25lciI6ImFkbWluIiwidGllclF1b3RhVHlwZSI6InJlcXVlc3RDb3VudCIsInRpZXIiOiJVbmxpbWl0ZWQiLCJuYW1lIjoiRGVmYXVsdEFwcGxpY2F0aW9uIiwiaWQiOjEsInV1aWQiOm51bGx9LCJzY29wZSI6ImFtX2FwcGxpY2F0aW9uX3Njb3BlIGRlZmF1bHQiLCJpc3MiOiJodHRwczpcL1wvbG9jYWxob3N0Ojk0NDNcL29hdXRoMlwvdG9rZW4iLCJ0aWVySW5mbyI6eyJQbGF0aW51bSI6eyJ0aWVyUXVvdGFUeXBlIjoicmVxdWVzdENvdW50Iiwic3RvcE9uUXVvdGFSZWFjaCI6dHJ1ZSwic3Bpa2VBcnJlc3RMaW1pdCI6NSwic3Bpa2VBcnJlc3RVbml0Ijoic2VjIn19LCJrZXl0eXBlIjoiUFJPRFVDVElPTiIsInN1YnNjcmliZWRBUElzIjpbeyJzdWJzY3JpYmVyVGVuYW50RG9tYWluIjoiY2FyYm9uLnN1cGVyIiwibmFtZSI6IlBpenphU2hhY2siLCJjb250ZXh0IjoiXC9waXp6YXNoYWNrXC8xLjAuMCIsInB1Ymxpc2hlciI6ImFkbWluIiwidmVyc2lvbiI6IjEuMC4wIiwic3Vic2NyaXB0aW9uVGllciI6IlBsYXRpbnVtIn1dLCJjb25zdW1lcktleSI6IlRFc3YyREZlYWlGbG5SdUp6dWMyTFp3cUN4d2EiLCJleHAiOjE1OTc2NjM5OTIsImlhdCI6MTU5NzY2MDM5MiwianRpIjoiNzRjOWVkZDMtZGUzYy00N2I0LTljNzctM2VhNTNkYzA1ZTVkIn0.GTtpRd3ke3jMF1Y7cSKRoLuR0nm-fTiubqh2OgFvVzalzRmWt8mNFb_cOjmkJc0fvoiP9nuylymDiHUEv6WpuWnYhjv_DSk7GQ6dDJWO23PUBduaEGUanfjWDvOcS9PjpDN6Ao5Zu2zkeePQwNYxLSarlmh38U4iSff0P0jVpxr7NDw31r_lPOeNkDyN0rG4k2qVoCRJGzYLbUYjlXQJoAJnIrgv7DYxqzOJpMxUiIOAi3uAMWyycnOxyR13CV9RsbYX5jt19C2Xc8_zz2EW76Hzp4mLoMLXaJ4HLe5DLo9wjPsGEcHwVJtzZ5-vUCNzwHecxZ7SxGfuFHpHCgiJew",
"scope": "am_application_scope default",
"token_type": "Bearer",
"expires_in": 3600
}
Now, i have used the below line to to do base64 encode of the above payload.
<property name="FinalEncodedResponse" scope="default" expression="base64Encode($body)"/>
The above line encodes the data but when i decode it appears as below (contains only values) and not the actual json that i specified above.
1012967TarunFinanceeyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5UZG1aak00WkRrM05qWTBZemM1TW1abU9EZ3dNVEUzTVdZd05ERTVNV1JsWkRnNE56YzRaQT09In0.eyJhdWQiOiJodHRwOlwvXC9vcmcud3NvMi5hcGltZ3RcL2dhdGV3YXkiLCJzdWIiOiJhZG1pbkBjYXJib24uc3VwZXIiLCJhcHBsaWNhdGlvbiI6eyJvd25lciI6ImFkbWluIiwidGllclF1b3RhVHlwZSI6InJlcXVlc3RDb3VudCIsInRpZXIiOiJVbmxpbWl0ZWQiLCJuYW1lIjoiRGVmYXVsdEFwcGxpY2F0aW9uIiwiaWQiOjEsInV1aWQiOm51bGx9LCJzY29wZSI6ImFtX2FwcGxpY2F0aW9uX3Njb3BlIGRlZmF1bHQiLCJpc3MiOiJodHRwczpcL1wvbG9jYWxob3N0Ojk0NDNcL29hdXRoMlwvdG9rZW4iLCJ0aWVySW5mbyI6eyJQbGF0aW51bSI6eyJ0aWVyUXVvdGFUeXBlIjoicmVxdWVzdENvdW50Iiwic3RvcE9uUXVvdGFSZWFjaCI6dHJ1ZSwic3Bpa2VBcnJlc3RMaW1pdCI6NSwic3Bpa2VBcnJlc3RVbml0Ijoic2VjIn19LCJrZXl0eXBlIjoiUFJPRFVDVElPTiIsInN1YnNjcmliZWRBUElzIjpbeyJzdWJzY3JpYmVyVGVuYW50RG9tYWluIjoiY2FyYm9uLnN1cGVyIiwibmFtZSI6IlBpenphU2hhY2siLCJjb250ZXh0IjoiXC9waXp6YXNoYWNrXC8xLjAuMCIsInB1Ymxpc2hlciI6ImFkbWluIiwidmVyc2lvbiI6IjEuMC4wIiwic3Vic2NyaXB0aW9uVGllciI6IlBsYXRpbnVtIn1dLCJjb25zdW1lcktleSI6IlRFc3YyREZlYWlGbG5SdUp6dWMyTFp3cUN4d2EiLCJleHAiOjE1OTc2ODgxNjAsImlhdCI6MTU5NzY4NDU2MCwianRpIjoiM2U5YjQ3YjUtY2I5ZC00MWVmLTgxMDktNWRhYjNiMzQxMjI4In0.fWzi4wWMeZjMGX92eczshPzRSA6v2lj47tpz69_u0MWuxhbQ0H4feJuDobwDqJCoQVE5lXmKMssprIgN4wZzeBEGxAzswJZZlnJD04QualzRhjXdBqyrLTFh01jnLKCzdowjhCDBokP5phHDfbSKqLZrLLHe5FpJw-wX0aGCe_rYpOcu9xwhYeV6UM0YLBK51Z3U-n3k6YvUwj9CZrPRqHfy1XWQiUnnGD7GyBiiN0I47ykuYYbT7jSh7cXX1N6lrSgc29oUcAZQtPr8w3LlTR-CipUrLfLgwh5Dn27M4Kh9mEeKhQddOlaFLeR4tyQmnIrtqPYmBjHSDH9vkR6ajgam_application_scope defaultBearer3600
I wanted to know how can we encode a json payload to base64 format as it is in WSO2? Need help with this.
Can you try the following approach?
<property expression="json-eval($)" name="messageBody" scope="default"/>
<property expression="base64Encode(get-property('messageBody'))"
name="FinalEncodedResponse"/>
Related
I am trying to transform json to xml using datamapper and i am getting the empty xml structure but not getting any values in wso2. I have created data mapper dependencies and loaded both input and out structures and used AI to map json -> xml and they mapped correctly.
I tried on eclipse oxygen(esb-6.2.0) and integration studio(v8) also and deployed in EI(6.5.0) but still the behavior is same empty response structure. I kept a log in the in-sequence and it is logging the json request but not the xml response. I am not getting why the issue is happening.
Please provide your thoughts and attached the code to this post. Please do needful
The issue is with the API. In the API we need to specify the input and the output data type. In your case since the transformation is from JSON to XML, the input type needs to be JSON while the output type needs to be XML. But you have defined both as xml which resulted in this issue. Modify the input type to JSON and you will get the expected results
<api xmlns="http://ws.apache.org/ns/synapse" name="DataMapper" context="/data">
<resource methods="POST" url-mapping="/mapper">
<inSequence>
<log separator="/">
<property name="payload" expression="json-eval($.body)"/>
</log>
<datamapper config="gov:datamapper/Wso2DataMapper.dmc" inputSchema="gov:datamapper/Wso2DataMapper_inputSchema.json" outputSchema="gov:datamapper/Wso2DataMapper_outputSchema.json" xsltStyleSheet="gov:datamapper/Wso2DataMapper_xsltStyleSheet.xml" inputType="JSON" outputType="XML"/>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
input
{
"studNo":"Sample studNo",
"studName":"Sample studName",
"StudGroup":"Sample StudGroup",
"studAddress":{
"addrLine1":"Sample addrLine1",
"AddrLine2":"Sample AddrLine2",
"Mandal":"Sample Mandal",
"District":"Sample District",
"State":"Sample State",
"Country":"Sample Country",
"Zip":"Sample Zip"
}
}
output
<Student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<studentNo>Sample studNo</studentNo>
<studentName>Sample studName</studentName>
<studentGroup>Sample StudGroup</studentGroup>
<studentAddress>
<AddressLine1>Sample AddrLine2</AddressLine1>
<AddressLine2>Sample addrLine1</AddressLine2>
<Mandal>Sample Mandal</Mandal>
<District>Sample District</District>
<State>Sample State</State>
<Country>Sample Country</Country>
<ZIP>Sample Zip</ZIP>
</studentAddress>
</Student>
I am using WSO2 APIM version 3.2.0.
I have a POST request with the request payload.
In the response message mediation of WSO2 APIM I have added the policy that contains the class mediator that tries to get the payload sent during the request.
OMElement element = (OMElement) mc.getEnvelope().getBody().getFirstOMChild();
log.info("payload: " + element.toString());
The above code snippet prints the response payload content but I need the request payload content at the response path.
Response message mediation with a policy added
Below is the sequence with class mediator
sequence with class mediator
Code snippet inside class mediator
OMElement element = (OMElement) mc.getEnvelope().getBody().getFirstOMChild();
log.info("payload: " + element.toString());
Pls let me know what changes to be done, to get the request payload content.
First, we have to store the request payload in a custom property in the Message Context. Then, we can use that property to retrieve the Request Payload in the Response path of the execution.
For example: You are invoking an API with JSON Payload. So, we have to first capture the sent payload and store it in a custom property in the Message Context. Given below is a sample sequence to perform the same
<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="admin--MockAPI:v1.0.0--In">
<property name="RequestPayload" expression="json-eval($)" />
<log level="custom">
<property name="RequestPayload" expression="$ctx:RequestPayload" />
</log>
</sequence>
Then, in the Response path, inside your custom class mediator, you have to access the RequestPayload property from the MessageContext to extract the stored payload. You can achieve this by using the following snippet
synapseContext.getProperty("RequestPayload");
here i am passing this payload { "cameraId": "1595", "fromDate": "2021-06-21 09:00:00", "toDate": "2021-06-21 09:00:10", "fileName": "test3" }
to endpoint directly ,it will give true response as
{
"clipUrl": "https://operator.smartkdcl.com/Videodownload/test3.mp4",
"status": true
}
but when i call endpoint from api , it will responds back with false as `
{
"status": false
}`
i tried with payload factory in api to pass this payload to endpoint but still false response.
it worked , just add <property name="DISABLE_CHUNKING" value="true" scope="axis2"/>
to code ,thank you
Does wso2 api manager v1.10.0 permit transforming the HTTP method of the request to the backend through custom in sequence?
I created an api with http GET resource through publisher web console. But since the endpoint support POST method only, i tried changing the HTTP Method by creating custom in sequence with property mediator :
<property name="HTTP_METHOD" value="POST" scope="axis2"/>
but the response showed a fault message :
{
"fault": {
"code": 403,
"type": "Status report",
"message": "Fault Call",
"description": "No matching resource found in the API for the given request"
}
}
The log files only showed these lines :
==> /opt/wso2am-gateway/repository/logs/wso2carbon.log <==
[2016-04-08 10:30:16,868] INFO - STATUS = Executing default 'fault' sequence, ERROR_CODE = 403, ERROR_MESSAGE = No matching resource found in the API for the given request {org.apache.synapse.mediators.builtin.LogMediator}
If i remove the property mediator, the request pass through and reach the backend.
Does anyone know how to solve this problem?
You can use the following custom inFlow mediation sequence to convert the HTTP_METHOD into POST from GET.
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="CustomIn" xmlns="http://ws.apache.org/ns/synapse">
<property action="remove" name="HTTP_METHOD" scope="axis2"/>
<property name="HTTP_METHOD" scope="axis2" type="STRING" value="POST"/>
</sequence>
Here, the request is the GET request from the client-side.
But, the above solution will be possible only when you are defining the GET and the POST resources similarly on your API (Although you don't use one of the both).
Otherwise, you will get the following error messages while you are invoking the API.
No matching resource found for given API Request
Method not allowed for given API resource
No matching resource found in the API for the given request
You need to also define POST resource on your API (although you don't use it)
I am retrieving the single row from DSS like
<Body xmlns="http://ws.wso2.org/dataservice">
<Datalist>
<username>anil</username>
<password>anil123</password>
</Datalist>
</Body>
and i am consuming this row in ESB Payload factory , while running the my ESB service,
i am getting like this :-
{"ResponseJSON":{"Body":{"Datalist":{"username":"anil","password":"anil123"}},"Status":"200","Total":"1.0"}}
But my output look like as JSON array.
{
"ResponseJSON": {
"Body": {
"Datalist": [
{
"username": "anil",
"password": "anil123"
}
]
},
"Status": "200",
"Total": "1.0"
}
}
How can we achieve this
I am also facing same issue in WSO2ESB 4.8.0
But got workaround by using Script-mediator.
Please let me know if you have any other solution for this problem.
Thanks
HI Add the following property in your Proxy Service sequence.
<property name="messageType" value="application/json" scope="axis2"/>
or else if you are working on WSO2 ESB 4.7.0.
<property name="ContentType" value="application/json" scope="axis2"/>
then no need to write above properties.It is automatically converted into json format.
Try to work on latest WS02 E.S.B 4.7.0, it's working fie.
If you need anything let me know.