API Connect

API Connect

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  Switch condition on Context Variable

    Posted Tue November 23, 2021 09:33 PM
    Hi, 
    In API Connect (v10.0.3 - API Gateway), I am trying to define a switch condition based on the context variable that I have defined in the previous gateway-script. context.set('id', '2');

    In below example, I am checking for the value of 'id' when set as a header, and this works fine. 

    1. How can I do the same when the 'id' is set as a context variable? 
    2. How can I do this when 'id' is set within a JSON body ({"id": 2}) defined within a context variable?

          - switch:
              version2.0.0
              titleswitch
              case:
                - condition($header("id") = '2')
                  execute[]



  • 2.  RE: Switch condition on Context Variable

    Posted Mon November 29, 2021 10:43 AM

    Hi Romil,

    Referencing the context variable directly is all that is needed

          - switch:
              version2.0.0
              titleswitch
              case:
                - condition(id = '2')
                  execute:
                   ...
    Regards,
    Steve


    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 3.  RE: Switch condition on Context Variable

    Posted Mon November 29, 2021 11:33 AM
    Thanks Steve, this worked. Directly using the variable name was probably the only thing I didn't try before  :)
    Really appreciate the help.


  • 4.  RE: Switch condition on Context Variable

    Posted Mon January 16, 2023 05:44 AM
    Hi @Steve Linn ,

    I request your help in the below issue , Please guide me.

    I am trying to set a runtime variable which I want to access in the set variable policy just after my gateway script , In my gateway script I have mentioned below code :

    var originalAPIpath = "/proposalservice/v2/proposal";
    var APIpath = originalAPIpath.replace("/proposalservice/v2", "");
    context.set('APIpath', APIpath);

    But I am getting an exception while doing this and it says in Datapower logs :

    apim.gatewayscript: ReferenceError: context is not defined

    Could you please let me know what I am doing wrong?
    Also I will be accessing the variable APIpath in the set variable policy below , is it correct?


    Thanks
    Adarsh



    ------------------------------
    Adarsh Thakur
    ------------------------------



  • 5.  RE: Switch condition on Context Variable

    Posted Mon January 23, 2023 10:59 AM

    Hi Adarsh,

    Is your posted code in a function?  This could be a scoping issue where the function does not know variable context.  It should be available implicitly in your mainline code but is not global to the entire gatewayscript code.  If within a function you should simply pass context as an argument to your function.  If your code is in the mainline of your code, the only thing I can think of is your gatewayscript policy is a version 1.0.0 which doesn't support the context variable.

    Regards,

    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------