WSO2 EI - how to handle malformed JSON requests in API - api

I have created an API in WSO2 EI.
API is expecting the requests to be send as JSON.
If it is a malformed JSON which is sent, then I should be able to receive it and make appropriate changes to make it a proper JSON.
Any help is much appreciated.
As we are using json-eval($.) to get the message to property, it is not working for me. What else i could try instead of json-eval to get the message as string.

WSO2 EI has the property force.json.message.validation, which can be used to validate incoming JSON payload. Please refer doc to configure this property in 'passthru-http.properties' file.
Once the property is configured, if there is a malformed JSON payload in the request, the mediation flow will be directed to the fault/error sequence. So, you can define the mediation logic to make appropriate changes to the malformed content within the fault sequence of your API.

Related

Fetch Request body in webclient when using Mono zip to identify which request fails

I am using webclient to call 3 different external APIs in parallel using Mono zip. Now, if any error occurs in any of the API call, I have handled it through a common exception handler using ExchangeFilterFunction. Now, I want to know which particular request fails(out of 3 external API calls) ? Is there any way I can identify that? I also want to append the 'input request body' with the exception response I got from the API call. Can I fetch the request body for which the API Fails (in filter itself)?
In the ExchangeFilterFunction you have access to the request. You could "decorate" your request with some identifier and then use it to resolve the client.
You could add some request header to identify the client.
To keep it internal, you could use request attributes of the WebClient and then read it from the request object
webClient.get()
.uri("...")
.attributes((attributes) -> attributes.put("internal_client_id", clientId))

Identify Operation on the basis of xml content posted in WCF Service without including operation name in Url

How to identify operation from xml content posted to WCF Service Url?
Suppose WCF Service Url is http://single.mat.nn.com and client dont want to include operation name in Url.
Problem is to identify operation on the basis of xml content posted .
I am not able to find any solution for this problem. Is it feasible to do configuration in WCF Service that can identify operation method on the basis of xml content posted to WCF Service URL.
One of the scenarios possible in Extending Dispatchers is:
Custom Operation Dispatching. Users can implement dispatching on something other than action – for example, on the body element, or on a custom message property. This can be done using the IDispatchOperationSelector interface.
Implmenting IDispatchOperationSelector will give you access to the incoming message to parse and decide which method you want to forward the request to.
The SOAP web service based on the corresponding method of the SOAPAction field request in the HTTP request. See the screenshot below.
The SOAPAction field and the method section in the request body can view the operation name of the specific request. If you want to recognize this value, we can intercept the SOAP message through the following two interfaces and get the value of the field.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/extending/how-to-inspect-or-modify-messages-on-the-client
https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.dispatcher.idispatchmessageinspector?view=netframework-4.8
these two interfaces could capture the SOAP message during the communication. We could retrieve the field value and modify it.
Feel free to let me know if there is anything I can help with.

Mule ESB Flow to pass parameters in Calling SOAP Webservice

I have created on flow in MuleESB which is calling a web-service without any parameter just sending it username, password and token in a property and it is working fine.
But the second API I want to post some parameters while calling soap request but I don't know how to use it I tried to pass through set payload but no response.
use Webservice consumer and add a transform message component beofre it. by doing so you can automatically map all the parameters which are required by the SOAP webservice, as datasence will automaticall download the meta deta of the service using the WSDL file.
Make sure you select application/xml as content type in Postman or SOAP UI and select POST.
Use CXF and select Operation as Proxy Service ,Provide details. Selct and provode (WSDL,MTOM enabled,SOAP Headers ,SOAP 1.2)
Make sure you posting XML request "POST" method in allowed methods.
Use 2 transformers. XML to DOM and DOM to XML.
Log the request using
#[message.payloadAs(java.lang.String)]
Use a groovy script transformer to retreive the entire payload.
def userSoapRequest = new XmlSlurper().parseText(payload);
def userId = userSoapRequest.userId.text();
message.setInvocationProperty('userId', userId);
6.Retrieve userId like above and similarly for all the elements.
7.Process them as you want.
Hope this helps

Mule Cache Scope Error com.hazelcast.nio.serialization.HazelcastSerializationException

I am using Mule Cache scope to save JSON data (converted to string) retrieved from a REST call. All works fine locally but when deployed to the server which is clustered I get this error.
com.hazelcast.nio.serialization.HazelcastSerializationException: java.io.NotSerializableException: com.rabbitmq.client.impl.ChannelN
Not sure why it is trying to serialize rabbitmq channelN.
When cache scope includes any Rest calls (i.e HTTP Request configurations) , make sure you have Dataweave or Byte array to String or Object to String to make it as consumable payload. Because HTTP request configuration output will be "grizzly Bufferinputstream" which is not consumable payload and its not serializable

Receive a http post in BizTalk orchestration

I have a wcf restful service that accepts http posts.
I am trying to send this to an orchestration with a rcv message set as XmlDocument. (Since no subscribers where found when set to system.string)
The message is received in the orchestration but the xmldocument is invalid so its not possible to do anything with the data(Im sending a string in the http post). (When I attempt it I get an error that the data at the root level is invalid).
How can I get the http post string into my orchestration? Do I need to add a custom pipelinecomponent that adds xml tags to the string to make it a valid XmlDocument?
My solution:
Created a flat file schema where the string in the http post is added to one line. Added a pipeline with a flat file dissasembler with the schema. Use xpath in orchestration to retrieve the string.