wso2 / product-ei

An open source, a high-performance hybrid integration platform that allows developers quick integration with any application, data, or system.
https://wso2.com/integration/
Apache License 2.0
375 stars 280 forks source link

Not able to make a rest api call using call mediator #5307

Closed sreenath-centime closed 2 weeks ago

sreenath-centime commented 3 years ago

REST API call failing using a Call Mediator in blocking mode

I am calling a NetSuite Rest Api call, using a call mediator in blocking mode. I am seeing the below error... [2020-11-05 01:08:36,009] INFO {org.apache.axis2.builder.BuilderUtil} - OMException in getSOAPBuilder org.apache.axiom.om.OMException: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<' at [row,col {unknown-source}]: [1,1] at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:296) at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.getSOAPEnvelope(StAXSOAPModelBuilder.java:204) .................................. ................................... Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<' at [row,col {unknown-source}]: [1,1] at com.ctc.wstx.sr.StreamScanner.throwUnexpectedChar(StreamScanner.java:639) at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2052) ................................ .................................. [2020-11-05 01:08:36,015] INFO {org.apache.axis2.builder.BuilderUtil} - Remaining input stream :[ther Current Asset","acctnumber":"1090","fullname":"Undeposited Funds","id":"6","inventory":"F","isinactive":"F"},{"links":........ ..........] [2020-11-05 01:08:36,016] WARN {API_LOGGER.TestAPI} - ERROR_CODE : 401002 ERROR_MESSAGE : com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<' at [row,col {unknown-source}]: [1,1]

I see partial input in the logs. I have set all the required headers like Content-Type : application/json, Accept : application/json, Authorization Header and also messageType property to application/json.

I noticed that the content type in the response headers is application/vnd.oracle.resource+json; type=collection; charset=UTF-8 and this could be the issue for not parsing the response I guess, but not able to resolve this issue.

Could you please help me resolve this issue as this is blocking my work, I am not able to proceed further due to this issue.

I am working on IntegrationStudio 7.0.0 version

I am able to make the call and get the response from POSTMAN. See the api call below curl --location --request POST 'https://.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql?limit=50&offset=0' \ --header 'prefer: transient' \ --header 'Authorization: OAuth .............' \ --header 'Cookie: NS_ROUTING_VERSION=LAGGING' \ --data-raw '{ "q": " select a.id, a.accountsearchdisplayname AS accountname, a.fullname, BUILTIN.DF(a.accttype) AS accounttype, a.parent, c.symbol AS currencycode, a.isinactive, a.acctnumber, a.sbankcompanyid, a.sbankname, a.sbankroutingnumber, a.location, a.department,a.class, a.inventory from account AS a LEFT OUTER JOIN currency AS c ON a.currency=c.id" }'

arunans23 commented 3 years ago

In axis2.xml try defining the correct message builder for your response header 'application/vnd.oracle.resource+json'. <messageBuilder contentType="application/vnd.oracle.resource+json" class="org.wso2.carbon.integrator.core.json.JsonStreamBuilder"/>

sreenath-centime commented 3 years ago

@arunans23 I have tried setting the in axis2.xml file at this location : /Applications/IntegrationStudio.app/Contents/Eclipse/runtime/microesb/conf/axis2/axis2.xml. However I changed the class org.wso2.micro.integrator.core.json.JsonStreamBuilder, instead of org.wso2.carbon.integrator.core.json.JsonStreamBuilder. I still see the same error.

Observation1 : After setting this property and restarting my carbon application from Integration Studio, in the logs i can see that its says change in axis2.xml file, but after the application gets restarted, the axis2.xml file goes back to its previous state without the changes I have added.

Observation2: I see that there are 2 other axis2_client.xml and axis2_blocking_client.xml files at the same location. Should I also change at the same location.

Observation3: Should I try adding the deployment.toml file and see if it gets picked? Observation4: Should we also try setting the messageFormatter instead of messageBuilder or both?

Please let me know, how to proceed further on this issue.

arunans23 commented 3 years ago

Forgive me for not noticing that you are using a call mediator in blocking mode. In that case, you need to add the entry in axis2_blocking_client.xml. Since you are using Micro Integrator (embedded or otherwise), you need to make the change in the deployment.toml. Try adding the following entry in the deployment.toml.

[[custom_message_builders.blocking]]
content_type = "application/vnd.oracle.resource+json"
class="org.wso2.micro.integrator.core.json.JsonStreamBuilder"

To be on the safe side add the Formatter too.

[message_formatters.blocking]
content_type = "application/vnd.oracle.resource+json"
class="org.wso2.micro.integrator.core.json.JsonStreamFormatter"

Note: In the past, there was an in issue https://github.com/wso2/micro-integrator/issues/1130 which restricted defining custom builder formatter in deployment.toml. Incase, the above approach did not work, try directly adding to the axis2_blocking_client.xml. It is recommended to use Integration Studio 7.1.0, because it comes with the latest Micro Integrator which has numerous number of fixes.

sreenath-centime commented 3 years ago

@arunans23 I tried the following steps

  1. Upgraded to 7.1.0 version IntegrationStudio, tried to set the following properties in deployment.toml file

    [[custom_message_builders.blocking]] content_type = "application/vnd.oracle.resource+json" class="org.wso2.micro.integrator.core.json.JsonStreamBuilder"

[custom_message_formatters.blocking] content_type = "application/vnd.oracle.resource+json" class="org.wso2.micro.integrator.core.json.JsonStreamFormatter"

The micro integrator throwing the below error on startup itself [2020-11-06 01:05:06,073] WARN {org.wso2.config.mapper.ConfigParser} - Configurations Changed in :conf/deployment.toml [2020-11-06 01:05:10,631] FATAL {org.wso2.micro.integrator.core.internal.CoreServerInitializer} - WSO2 Carbon initialization Failed org.apache.axis2.AxisFault: Exception occured while loading the Axis configuration from /Applications/IntegrationStudio.app/Contents/Eclipse/runtime/microesb/conf/axis2/axis2.xml at org.wso2.micro.core.CarbonAxisConfigurator.getAxisConfiguration(CarbonAxisConfigurator.java:172)... ...... Caused by: org.apache.axis2.deployment.DeploymentException: A Invalid Class Name error occurred while validating the message builder .........

  1. Changed the axis2_blocking_client.xml file directly and added, messageBuilder contentType="application/vnd.oracle.resource+json" class="org.wso2.micro.integrator.core.json.JsonStreamBuilder"/> messageFormatter contentType="application/vnd.oracle.resource+json" class="org.wso2.micro.integrator.core.json.JsonStreamFormatter"/>

Also tried with different class, org.apache.synapse.commons.json.JsonStreamFormatter and org.apache.synapse.commons.json.JsonStreamBuilder, as that is how it was for existing property messageFormatter contentType="application/json" class="org.apache.synapse.commons.json.JsonStreamFormatter"/>, but seeing the same old error like below [2020-11-06 01:15:28,951] INFO {org.apache.axis2.builder.BuilderUtil} - OMException in getSOAPBuilder org.apache.axiom.om.OMException: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<' at [row,col {unknown-source}]: [1,1] at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:296) ....... .... Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<' at [row,col {unknown-source}]: [1,1] ............. ............. [2020-11-06 01:15:28,955] INFO {org.apache.axis2.builder.BuilderUtil} - Remaining input stream :[ther Current Asset","....] [2020-11-06 01:15:28,957] WARN {API_LOGGER.TestAPI} - ERROR_CODE : 401002 ERROR_MESSAGE : com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<' at [row,col {unknown-source}]: [1,1] [2020-11-06 01:15:28,958] WARN {org.apache.synapse.endpoints.EndpointContext} - Endpoint : NetsuiteEntityEP with address https://tstd123456.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql?limit=50&offset=0 will be marked SUSPENDED as it failed

I have tried with 7.1.0 and by changing in deployment.toml and axis2_blocking_client.xml files Attaching the sample sequence I am using. Kindly look into it and help me resolve this issue. CallNetsuiteEntityEndpoint.xml.zip

arunans23 commented 3 years ago

@sreenath-centime It seems there is a grave mistake in the documentation. The correct way to define custom message builder would be as follows. Sorry about the inconvenience caused. We'll get it fixed in the docs.

[[blocking.custom_message_builders]]
content_type = "application/vnd.oracle.resource+json"
class = "org.wso2.micro.integrator.core.json.JsonStreamBuilder"
arunans23 commented 3 years ago

Created a doc issue to track the issue. https://github.com/wso2/docs-ei/issues/2435

sreenath-centime commented 3 years ago

@arunans23 I made the changes, added the following lines to the deployment.toml file

[[blocking.custom_message_builders]] content_type = "application/vnd.oracle.resource+json" class = "org.wso2.micro.integrator.core.json.JsonStreamBuilder"

[[blocking.custom_message_formatters]] content_type = "application/vnd.oracle.resource+json" class="org.wso2.micro.integrator.core.json.JsonStreamFormatter"

But still the same issue. Could you please check the sequence I attached in my previous post, I am basically calling a POST api in blocking mode using call mediator and setting the Conteny-Type Header to application/json along with other required headers. This is my use case. I am seeing the response partially in the logs, below line frm logs confirms that

[2020-11-06 10:24:09,685] INFO {org.apache.axis2.builder.BuilderUtil} - Remaining input stream :[ther Current Asset","acctnumber":"1090",........]

But somehow it is not able to parse the response. Please suggest any other changes I need to make to fix this issue.

sreenath-centime commented 3 years ago

Hi @arunans23 I changed the custom message builders as below

[[custom_message_builders]] content_type = "application/vnd.oracle.resource+json" class = "org.wso2.micro.integrator.core.json.JsonStreamBuilder"

[[custom_message_formatters]] content_type = "application/vnd.oracle.resource+json" class="org.wso2.micro.integrator.core.json.JsonStreamFormatter"

And also changed my call mediator to non-blocking, the call was successful. But this may have impact in our other functionality, as the following mediators after the call mediator are behaving differently as we have a complex data pipeline implementation and we wanted to make sure that the calls go synchronously and in the sequential order.

I don't completely understand the non-blocking impact, but I definitely see some changes in our flow. Kindly let me know if you have a solution or a workaround to have our calls made in blocking mode and with the blocking custom message builders.Thanks.

TomasTokaMrazek commented 3 years ago

Blocking mode should not impact the flow. In other words, mediation should not continue after calling call mediator before response is received. There is an exception, if parameter OUT_ONLY and/or FORCE_SC_ACCEPTED is set.

In both blocking and non-blocking modes, Call mediator behaves in a synchronous manner. Hence, mediation pauses after the service invocation, and resumes from the next mediator in the sequence when the response is received.

sreenath-centime commented 3 years ago

Hi @TomasTokaMrazek @arunans23 I am still facing the above issue, could you kindly help me resolve this issue. I am using WSO2 Intergration Studio 7.1.0. To reiterate my usecase, I am making a netsuite rest call using call mediator in blocking mode and the netsuite call, has the following response header, Content-Type : application/vnd.oracle.resource+json; type=collection; charset=UTF-8, When I make the call in non blocking mode, it is working fine, but my flow will have issues as non blocking flow will be asynchronous and I need to update some flags after I get the response from the call mediator and non blocking mode might not serve the purpose. When I am making the call in blocking mode, it is failing with the error

[2021-08-30 09:05:35,095] INFO {org.apache.axis2.builder.BuilderUtil} - OMException in getSOAPBuilder org.apache.axiom.om.OMException: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<' at [row,col {unknown-source}]: [1,1] at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:296) ....... .... Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<' at [row,col {unknown-source}]: [1,1] ......... ..... [2021-08-30 09:05:35,106] INFO {org.apache.axis2.builder.BuilderUtil} - Remaining input stream :[service@aviconsulting.com","fax":"+61 2 9256 1244","id":"1471","phone":"+61 2 9256 1221","terms":"2"},{"links":[],"activestatus":"1","altname":"Alexander Valley ........ ...........] I am getting the response with truncated data in the last line.

Tried different ways to set the custom message builders,

  1. updating the axis2.xml file <messageBuilder contentType="application/vnd.oracle.resource+json" class="org.wso2.micro.integrator.core.json.JsonStreamBuilder"/> <messageFormatter contentType="application/vnd.oracle.resource+json" class="org.wso2.micro.integrator.core.json.JsonStreamFormatter"/>

  2. updating the axis2_blocking_client.xml file <messageBuilder contentType="application/vnd.oracle.resource+json" class="org.wso2.micro.integrator.core.json.JsonStreamBuilder"/> <messageFormatter contentType="application/vnd.oracle.resource+json" class="org.wso2.micro.integrator.core.json.JsonStreamFormatter"/>

  3. updating the deployment.toml file - [[blocking.custom_message_builders]] content_type = "application/vnd.oracle.resource+json" class = "org.wso2.micro.integrator.core.json.JsonStreamBuilder"

[[blocking.custom_message_formatters]] content_type = "application/vnd.oracle.resource+json" class="org.wso2.micro.integrator.core.json.JsonStreamFormatter"

None of the above approaches are working and resulting in the same above error. One observation is after I update in the xml file, and restart the application, the custom values are disappearing from axis2 files after the application is started.

Kindly help us resolve this issue. @TomasTokaMrazek @arunans23