Layer7 API Management

Expand all | Collapse all

Context Variable for Hostname

Jump to Best Answer
  • 1.  Context Variable for Hostname

    Posted 11-23-2017 07:41 AM

    Hi guys I am using Gateway 9.1.

    My request URL to call services in Gateway is,

     

    https://api.sample.com/poc/users/saml/23478

     

    I want to get the above hostname(api.sample.com) in a variable. I tried using split variable assertion to split using "/"

    Then I am getting below response,

    ${split_request_uri[0]} - gives empty

    ${split_request_uri[1]} - poc
    ${split_request_uri[2]} - users
    ${split_request_uri[3]} - saml
    ${split_request_uri[4]} - 23478
    ${request.url} - https://api.sample.com/poc/users/saml/23478
    ${request.tcp.localHost} - gives Gateway Node IP which is not what I want.

     

    I also tried using split variable assertion to split using "//"

    Then I am getting below response,

    ${split_request_uri[1]} - /poc/users/saml/23478

     

    Any help is appreciated.

     

    Thanks



  • 2.  Re: Context Variable for Hostname

     
    Posted 11-23-2017 08:01 AM

    Hi, you could use the context variable ${request.url.host} to get the host part of the request url.

     

    See https://docops.ca.com/ca-api-gateway/9-2/en/reference/context-variables/transport-layer-context-variables for more information, look for ${request.url}

     

    Regards / Mikael



  • 3.  Re: Context Variable for Hostname
    Best Answer

    Posted 11-23-2017 07:36 PM

    Hi

     

    I would try this as your right the variable that you referenced within your post doesn't always work.  You can always try the following to pull the DNS name out.  I used the following request URL:

     

    http://test.api.ca.com:8080/group?test=CA&group=CAAPIM 

     

    I have 2 assertions within my policy:

     

    - Evaluate Regular expression

    - Return Template Response

     

    Regex assertion looks like this:

     

    Regular Expression field = //([^/?:]*)

    Source = request.url

    Destination section:

       - Button selected for "Proceed if Pattern Matches"

       - Context Variable = test

     

    Return Template response is:

     

    test:

    URL = ${request.url}
    URI = ${request.http.uri}

     

    Parameter:

    test=${request.http.parameter.test}
    group=${request.http.parameter.group}


    Regular expression:

    test =${test[1]}

     

     

    Variable test is what the result of the "Evaluate Regular Expression" assertion.

     

    When I ran this test I received the following:

    ===================================

    test: URL = http://test.api.ca.com:8080/group?test=CA&group=CAAPIM URI = /group  Parameter: test=CA group=CAAPIM  Regular expression: test =test.api.ca.com

    ======================================

     

    As well to take note here is how you can call back parameters within the URL.

     

    request.http.parameter.*

    *= name of parameter.

     

     

    Hope this helps?

     

    Regards



  • 4.  Re: Context Variable for Hostname

    Posted 11-23-2017 11:03 PM

    Thank you for the solution .