Forum Discussion

Vib's avatar
Vib
Occasional Contributor
5 years ago

Adding additiona query parameters to the request via ServiceV option “Route Non Virtualized Request"

We are facing issue with the routing scenario of the ServiceV option “Route Non Virtualized Request”.

As per our requirement once the virtual service receives the request, we have to add an additional query parameter and then route live system.

However, we are seeing the query parameters are getting repeated.

 

Below is the example:

 

Request sent to virtual service: POST http://localhost:8090/oauth2?username=ABCD&password=Test123

 

As per the requirement we have to add this additional parameter: env=ENV1 to query string

 

Therefore while routing the ServiceV should do the routing as http://LIVE HOST:PORT/oauth2?username=ABCD&password=Test123&env=ENV1

 

However, we are seeing double query parameters being sent.

  • Vib's avatar
    Vib
    5 years ago

    I was able to move ahead by adding the below event 

     

    MockRunListener.beforeRoute and added the below script, the routing is working as expected.

    method.setURI(URI.create(method.getURI().toString() + "&enviornment=ENV1"))

     

    Thanks Temil for providing the solution

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Could you please clarify:

    -- What parameter must be added: validator_id=DEX1 or env=ENV1?

    -- "double query parameters being sent" - what does this mean? Can you provide an example/screenshot?

    -- Considering that "double query parameters being sent", it looks like you were able to add additional parameter to the request before it is routed. Can you let us know how this was done?

     

    • Vib's avatar
      Vib
      Occasional Contributor

      Thanks for response and sorry for the confusion

      We are trying to add the add the additional parameter as env=ENV1
      The incoming request from the client looks like : http://localhost:8090/oauth2?username=ABCD&password=Test123
      The requirement is to add addtional parameter env=ENV1 to the incoming request like : http://localhost:8090/oauth2?username=ABCD&password=Test123&env=ENV1

      I tried to add the below script to the Events (however it did not work) and then later I tried to add the same script to the "OnRequest Script" where we are trying to extract the query parameters and then passed query property to the "Route to" option as : https://HOSTNAME?${query}&env=ENV1
      Please see the screen-shot.However, it did not work.

      def incomingRequest = mockRequest.httpRequest
      def path = mockRequest.path
      def query = mockRequest.queryString
      context.setProperty("query", query)

      Below is the error what I see in the http logs :

      POST /?scope=full&grant_type=password&username=ABCD&password=Test123&env=ENV1/as/token.oauth2?scope=full&grant_type=password&username=ABCD&password=Test123 HTTP/1.1

      I am new to the ReadyAPI ServiceV. If you think this can be achieved by a different method please help with it.

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        Thank you for the detailed description and the screenshot.

        It is my current understanding that you need this:

        -- If the request to your virtualized service contains /oauth2 in the path then extra parameter must be added to the query string and the request must be routed;

        -- Otherwise the request must not be routed.

         

        If my above understanding is correct, then I would try to follow the scenario described in this help topic: https://support.smartbear.com/readyapi/docs/servicev/routing/conditional.html

        I.e.:

        -- Set Route Options for the virtual service to either Route to or Route Non-Virtualized Requests according to what works best for you and as per https://support.smartbear.com/readyapi/docs/servicev/routing/about.html#setting-up-routing description;

        -- For the required request, set Routing Options to Conditional;

        -- Enter this code into the Routing Options script code window that will appear on the right:

        if (! mockRequest.path.contains('/oauth2'))
          return false; // do not route the request
        
        mockRequest.queryString += '&env=ENV1';
        log.info 'DEBUG: Modified query string - ' + mockRequest.queryString;
        return true; // route the request
        

        Does this help?