Cómo pasar el ampersand en json body en wso2 esb
rest wso2esb (2)
Tengo un problema al pasar un signo comercial dentro de un valor JSON de cuerpo al llamar a una API REST en WSO2 esb
, se da una excepción. Pero necesito pasar este valor ''&'' al backend a través de esb y no como &
.
Traté de enviarlo de las siguientes maneras, pero no tuve éxito.
-
&
- valor codificado de url
- valor hexadecimal
Porfavor ayudame a resolver este problema.
Esta es la excepción que obtuve.
[2016-08-10 13:52:26,538] ERROR - The script engine returned an error executing the inlined js script function mediate {org.apache.synapse.mediators.bsf.ScriptMediator}
com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EcmaError: TypeError: error: Unexpected character encountered (lex state 3): '' '' (<Unknown Source>#1) in <Unknown Source> at line number 1
at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:68)
at javax.script.CompiledScript.eval(CompiledScript.java:92)
at org.apache.synapse.mediators.bsf.ScriptMediator.mediateForInlineScript(ScriptMediator.java:291)
at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:235)
at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:203)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:77)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:47)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:131)
at org.apache.synapse.rest.Resource.process(Resource.java:297)
at org.apache.synapse.rest.API.process(API.java:341)
at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:76)
at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:63)
at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:220)
at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:83)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:344)
at org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:385)
at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:183)
at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.mozilla.javascript.EcmaError: TypeError: error: Unexpected character encountered (lex state 3): '' '' (<Unknown Source>#1)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3350)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3340)
at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3356)
at org.mozilla.javascript.xml.impl.xmlbeans.XML.createFromJS(XML.java:388)
at org.mozilla.javascript.xml.impl.xmlbeans.XML.jsConstructor(XML.java:3065)
at org.mozilla.javascript.xml.impl.xmlbeans.XMLObjectImpl.execIdCall(XMLObjectImpl.java:549)
at org.mozilla.javascript.IdFunctionObject.call(IdFunctionObject.java:127)
at org.mozilla.javascript.BaseFunction.construct(BaseFunction.java:313)
at org.mozilla.javascript.ScriptRuntime.newObject(ScriptRuntime.java:2132)
at org.mozilla.javascript.gen.c92._c0(<Unknown Source>:1)
at org.mozilla.javascript.gen.c92.call(<Unknown Source>)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834)
at org.mozilla.javascript.gen.c92.call(<Unknown Source>)
at org.mozilla.javascript.gen.c92.exec(<Unknown Source>)
at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
... 21 more
[2016-08-10 13:52:26,540] WARN - Executing fault handler due to exception encountered {org.apache.synapse.core.axis2.SynapseMessageReceiver}
[2016-08-10 13:52:26,540] WARN - ERROR_CODE : 0 {org.apache.synapse.FaultHandler}
[2016-08-10 13:52:26,541] WARN - ERROR_MESSAGE : The script engine returned an error executing the inlined js script function mediate {org.apache.synapse.FaultHandler}
[2016-08-10 13:52:26,541] WARN - ERROR_DETAIL : org.apache.synapse.SynapseException: The script engine returned an error executing the inlined js script function mediate
at org.apache.synapse.mediators.AbstractMediator.handleException(AbstractMediator.java:313)
at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:241)
at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:203)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:77)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:47)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:131)
at org.apache.synapse.rest.Resource.process(Resource.java:297)
at org.apache.synapse.rest.API.process(API.java:341)
at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:76)
at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:63)
at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:220)
at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:83)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:344)
at org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:385)
at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:183)
at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EcmaError: TypeError: error: Unexpected character encountered (lex state 3): '' '' (<Unknown Source>#1) in <Unknown Source> at line number 1
at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:68)
at javax.script.CompiledScript.eval(CompiledScript.java:92)
at org.apache.synapse.mediators.bsf.ScriptMediator.mediateForInlineScript(ScriptMediator.java:291)
at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:235)
... 18 more
Caused by: org.mozilla.javascript.EcmaError: TypeError: error: Unexpected character encountered (lex state 3): '' '' (<Unknown Source>#1)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3350)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3340)
at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3356)
at org.mozilla.javascript.xml.impl.xmlbeans.XML.createFromJS(XML.java:388)
at org.mozilla.javascript.xml.impl.xmlbeans.XML.jsConstructor(XML.java:3065)
at org.mozilla.javascript.xml.impl.xmlbeans.XMLObjectImpl.execIdCall(XMLObjectImpl.java:549)
at org.mozilla.javascript.IdFunctionObject.call(IdFunctionObject.java:127)
at org.mozilla.javascript.BaseFunction.construct(BaseFunction.java:313)
at org.mozilla.javascript.ScriptRuntime.newObject(ScriptRuntime.java:2132)
at org.mozilla.javascript.gen.c92._c0(<Unknown Source>:1)
at org.mozilla.javascript.gen.c92.call(<Unknown Source>)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834)
at org.mozilla.javascript.gen.c92.call(<Unknown Source>)
at org.mozilla.javascript.gen.c92.exec(<Unknown Source>)
at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
... 21 more
He intentado lo mismo con los registros de conexión habilitados y funcionó para mí sin ningún cambio. ¿Cuál es la versión de ESB que probaste? Si habilitó los registros de conexión, verá qué viene y qué sale de la siguiente manera. Agregué & symbol to body parameter.
[2016-08-10 11:11:05,643] DEBUG - wire >> "POST /pizzashack/1.0.0/order HTTP/1.1[/r][/n]"
[2016-08-10 11:11:05,643] DEBUG - wire >> "Host: 172.17.0.1:8243[/r][/n]"
[2016-08-10 11:11:05,643] DEBUG - wire >> "User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0[/r][/n]"
[2016-08-10 11:11:05,643] DEBUG - wire >> "Accept: application/json[/r][/n]"
[2016-08-10 11:11:05,643] DEBUG - wire >> "Accept-Language: en-US,en;q=0.5[/r][/n]"
[2016-08-10 11:11:05,643] DEBUG - wire >> "Accept-Encoding: gzip, deflate, br[/r][/n]"
[2016-08-10 11:11:05,643] DEBUG - wire >> "Content-Type: application/json[/r][/n]"
[2016-08-10 11:11:05,643] DEBUG - wire >> "Authorization: Bearer 0227aad0-bc8c-3a4a-b010-fadb7582b488[/r][/n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "Referer: https://127.0.0.1:9443/store/apis/info?name=PizzaShackAPI&version=1.0.0&provider=admin[/r][/n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "Content-Length: 169[/r][/n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "origin: https://127.0.0.1:9443[/r][/n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "Connection: keep-alive[/r][/n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "[/r][/n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "{[/n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> " "customerName": "string&test",[/n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> " "delivered": true,[/n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> " "address": "string",[/n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> " "pizzaType": "string",[/n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> " "creditCardNumber": "string",[/n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> " "quantity": 0,[/n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> " "orderId": 0[/n]"
[2016-08-10 11:11:05,644] DEBUG - wire >> "}"
[2016-08-10 11:11:05,647] DEBUG - wire << "POST /am/sample/pizzashack/v1/api/order HTTP/1.1[/r][/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Accept: application/json[/r][/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "origin: https://127.0.0.1:9443[/r][/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Referer: https://127.0.0.1:9443/store/apis/info?name=PizzaShackAPI&version=1.0.0&provider=admin[/r][/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Accept-Encoding: gzip, deflate, br[/r][/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Accept-Language: en-US,en;q=0.5[/r][/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Content-Type: application/json; charset=UTF-8[/r][/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Transfer-Encoding: chunked[/r][/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Host: localhost:9443[/r][/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "Connection: Keep-Alive[/r][/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "User-Agent: Synapse-PT-HttpComponents-NIO[/r][/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "[/r][/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "a9[/r][/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "{[/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << " "customerName": "string&test",[/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << " "delivered": true,[/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << " "address": "string",[/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << " "pizzaType": "string",[/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << " "creditCardNumber": "string",[/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << " "quantity": 0,[/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << " "orderId": 0[/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "}[/r][/n]"
[2016-08-10 11:11:05,647] DEBUG - wire << "0[/r][/n]"
[2016-08-10 11:11:05,648] DEBUG - wire << "[/r][/n]"
Intenté lo mismo en ESB 4.8.1 también. Desde que vi los errores de script java en su registro de errores también agregué el mediador de script java. Por favor, consulte la muestra de API a continuación.
<api name="TestAPI" context="/test">
<resource methods="POST" url-mapping="/status" faultSequence="fault">
<inSequence>
<script language="js">var symbol = mc.getPayloadXML()..*::password.toString();
mc.setPayloadXML(
<m:getQuote xmlns:m="http://services.samples/xsd">
<m:request>
<m:symbol>{symbol}</m:symbol>
</m:request>
</m:getQuote>);</script>
<send>
<endpoint name="test-I_APIproductionEndpoint_0">
<http uri-template="http://127.0.0.1:8888/"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</resource>
</api>
Entonces envío la solicitud de la siguiente manera.
curl -v -X POST -H "Content-Type: application/json" -d ''{"username":"xyz","password":"xyz & abc"}'' http://127.0.0.1:8280/test/status
Puedo ver que mis valores pasan al final correctamente. Solo para verificar que agregué TCPMon entre ESB y back-end para verificar el mensaje de salida. Está volviendo con & signo. Los registros de cables están aquí y también se adjunta la salida de TCPMon.
[2016-08-10 14:30:21,236] DEBUG - wire >> "POST /test/status HTTP/1.1[/r][/n]"
[2016-08-10 14:30:21,236] DEBUG - wire >> "Host: 127.0.0.1:8280[/r][/n]"
[2016-08-10 14:30:21,236] DEBUG - wire >> "User-Agent: curl/7.43.0[/r][/n]"
[2016-08-10 14:30:21,236] DEBUG - wire >> "Accept: */*[/r][/n]"
[2016-08-10 14:30:21,236] DEBUG - wire >> "Content-Type: application/json[/r][/n]"
[2016-08-10 14:30:21,236] DEBUG - wire >> "Content-Length: 41[/r][/n]"
[2016-08-10 14:30:21,237] DEBUG - wire >> "[/r][/n]"
[2016-08-10 14:30:21,237] DEBUG - wire >> "{"username":"xyz","password":"xyz & abc"}"
[2016-08-10 14:30:21,245] DEBUG - wire << "POST /status HTTP/1.1[/r][/n]"
[2016-08-10 14:30:21,245] DEBUG - wire << "Content-Type: application/json[/r][/n]"
[2016-08-10 14:30:21,246] DEBUG - wire << "Accept: */*[/r][/n]"
[2016-08-10 14:30:21,246] DEBUG - wire << "Transfer-Encoding: chunked[/r][/n]"
[2016-08-10 14:30:21,246] DEBUG - wire << "Host: 127.0.0.1:8888[/r][/n]"
[2016-08-10 14:30:21,246] DEBUG - wire << "Connection: Keep-Alive[/r][/n]"
[2016-08-10 14:30:21,246] DEBUG - wire << "User-Agent: Synapse-PT-HttpComponents-NIO[/r][/n]"
[2016-08-10 14:30:21,246] DEBUG - wire << "[/r][/n]"
[2016-08-10 14:30:21,246] DEBUG - wire << "2f[/r][/n]"
[2016-08-10 14:30:21,247] DEBUG - wire << "{"getQuote":{"request":{"symbol":"xyz & abc"}}}[/r][/n]"
[2016-08-10 14:30:21,247] DEBUG - wire << "0[/r][/n]"
[2016-08-10 14:30:21,247] DEBUG - wire << "[/r][/n]"