Skip to main content

Handling special characters in URLs with WSO2 ESB

Let’s say you have a URL which needs to send an email address as a query parameter. If you create a standard REST API within WSO2 ESB (or EI) and try to match the query parameter in uri-template, it will fail during the execution time. As an example, if you have the below API definition.

<api xmlns="http://ws.apache.org/ns/synapse" name="EmailAPI" context="/voice">
   <resource methods="POST GET" uri-template="/details?email={email}">
      <inSequence>
         <log level="full">
            <property name="EMAIL" expression="$url:email"/>
         </log>
         <respond/>
      </inSequence>
   </resource>
   <resource methods="POST GET" uri-template="/phone/{number}">
      <inSequence>
         <log level="full">
            <property name="PHONE" expression="$ctx:uri.var.number"/>
         </log>
         <respond/>
      </inSequence>
   </resource>
</api>

You would expect that following request should work properly.

http://localhost:8280/voice/details?email=chanaka@gmail.com

If you hit the above URL, you will get a “404 not found” error. The reason is ESB cannot dispatch the request to matching resource since you have special character “@” in the URL path. You can find more information about these special characters from below link.

https://secure.n-able.com/webhelp/NC_9-1-0_SO_en/Content/SA_docs/API_Level_Integration/API_Integration_URLEncoding.html
 
In this post, I’m going to explain how you can resolve this special character issue using 2 approaches.

Solution 1


The first solution is to URL encode the special character when sending the request to this API. Now the request URL should look like below.

http://localhost:8280/voice/details?email=chanaka%40gmail.com

When you send the request with above URL, it will dispatch to the correct resource and you will see the log message with the proper email address similar to below mentioned log entry.

[2018-05-04 13:35:12,660] [EI-Core]  INFO - LogMediator To: /voice/details?email=chanaka%40gmail.com, MessageID: urn:uuid:281dcd97-2043-45a3-8842-d3a0b27f5efa, Direction: request, EMAIL = chanaka@gmail.com, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><name>chanaka</name></soapenv:Body></soapenv:Envelope>

Even though the incoming URL parameter is encoded, once it is accessed within the mediation sequence using $url syntax, it is giving the properly decoded value.

This solution is easier for the developers, but harder for the users of this API.

Solution 2


Instead of asking your users to change the client side implementation, we can handle this within the ESB itself using a simple trick. The trick is to use “*” to define the URL path when creating the API. You can create an API similar to the below configuration.

<api xmlns="http://ws.apache.org/ns/synapse" name="AsterixAPI" context="/voice">
   <resource methods="POST GET" uri-template="/details*">
      <inSequence>
         <log level="full">
            <property name="EMAIL" expression="$url:email"/>
            <property name="PHONE" expression="$url:phone"/>
         </log>
         <respond/>
      </inSequence>
   </resource>
   <resource methods="POST GET" uri-template="/phone*">
      <inSequence>
         <log level="full">
            <property name="PHONE" expression="$url:phone"/>
         </log>
         <respond/>
      </inSequence>
   </resource>
</api>

Here we have defined the matching uri-template with “*” so that it will capture all the requests coming into this context “/voice/details*”. Now if you send a request to following URL

http://localhost:8280/voice/details?email=chanaka@wso2.com&phone=(077)-3337238

In this URL, we have 3 special characters “@”, “(“, “)” but we are not asking the client to URL encode those parameters. Rather, user can send them as it is to the API and within the ESB mediation runtime, these query parameters are accessed using the $url syntax. Here we are specifically ignoring these special characters from template matching to avoid the special character impact. Now you can see a log similar to below if you send a request to the above URL.

[2018-05-04 14:20:57,841] [EI-Core]  INFO - LogMediator To: /test2/email?email=chanaka@gmail.com&phone=(077)-3337238, MessageID: urn:uuid:6926aa13-8f1d-40ef-afb9-02021d2d908a, Direction: request, EMAIL = chanaka@gmail.com, PHONE = (077)-3337238, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><email>chanaka@gmail.com</email></soapenv:Body></soapenv:Envelope>

In the above log, you can clearly see that email address and phone number query parameters are accessed within the mediation sequence in the proper format.

These are the 2 solutions for the URL special character related requirements within WSO2 ESB or EI.

Comments

  1. Google has ever been the go-to location for internet translation, but it appears that the provider would like to take things to the next level. More information on Links - Anthony Teixeira - Professional French Translator.

    ReplyDelete
  2. Track Mobile Number

    We can give you the details of mobile number of 22 countries of the world along with the name and home address and location of the owner. The list of countries whose mobile number details are available for you is provided below:
    Australia, Bangladesh, Canada, China, Ghana, India, Indonesia, Kenya, Malaysia, Nepal, Norway, Nigeria, Philippines, Poland, Pakistan, Singapore, Suudi Arabia, South Africa, Sri Lanka, United Arab Emirates, United Kingdom, United States

    ReplyDelete

Post a Comment

Popular posts from this blog

WSO2 ESB tuning performance with threads

I have written several blog posts explaining the internal behavior of the ESB and the threads created inside ESB. With this post, I am talking about the effect of threads in the WSO2 ESB and how to tune up threads for optimal performance. You can refer [1] and [2] to understand the threads created within the ESB. [1] http://soatutorials.blogspot.com/2015/05/understanding-threads-created-in-wso2.html [2] http://wso2.com/library/articles/2012/03/importance-performance-wso2-esb-handles-nonobvious/ Within this blog post, I am discussing about the "worker threads" which are used for processing the data within the WSO2 ESB. There are 2 types of worker threads created when you start sending the requests to the server 1) Server Worker/Client Worker Threads 2) Mediator Worker (Synapse-Worker) Threads Server Worker/Client Worker Threads These set of threads will be used to process all the requests/responses coming to the ESB server. ServerWorker Threads will be used to pr

How puppet works in your IT infrstructure

What is Puppet? Puppet is IT automation software that helps system administrators manage infrastructure throughout its lifecycle, from provisioning and configuration to orchestration and reporting. Using Puppet, you can easily automate repetitive tasks, quickly deploy critical applications, and proactively manage change, scaling from 10s of servers to 1000s, on-premise or in the cloud. How the puppet works? It works like this..Puppet agent is a daemon that runs on all the client servers(the servers where you require some configuration, or the servers which are going to be managed using puppet.) All the clients which are to be managed will have puppet agent installed on them, and are called nodes in puppet. Puppet Master: This machine contains all the configuration for different hosts. Puppet master will run as a daemon on this master server. Puppet Agent: This is the daemon that will run on all the servers, which are to be managed using p

How to configure timeouts in WSO2 ESB to get rid of client timeout errors

WSO2 ESB has defined some configuration parameters which controls the timeout of a particular request which is going out of ESB. In a particular  scneario, your client sends a request to ESB, and then ESB sends a request to another endpoint to serve the request. CLIENT->WSO2 ESB->BACKEND The reason for clients getting timeout is that ESB timeout is larger than client's timeout. This can be solved by either increasing the timeout at client side or by decreasing the timeout in ESB side. In any of the case, you can control the timeout in ESB using the below properties. 1) Global timeout defined in synapse.properties (ESB_HOME\repository\conf\) file. This will decide the maximum time that a callback is waiting in the ESB for a response for a particular request. If ESB does not get any response from Back End, it will drop the message and clears out the call back. This is a global level parameter which affects all the endpoints configured in ESB. synapse.global_timeout_inte