API Connect

 View Only
Expand all | Collapse all

how to refer payloads in switch policy

  • 1.  how to refer payloads in switch policy

    Posted Mon February 14, 2022 04:18 PM
    Hi ,

    i need to route request to backend based on content of element in the request messages . could you please tell me how to do in switch policy in apiconnect v10. i could not find any examples in the below reference link.

    https://www.ibm.com/docs/en/api-connect/10.0.1.x?topic=switch-using-policy-condition-editor



    ------------------------------
    santhoshkumar surisetty
    ------------------------------


  • 2.  RE: how to refer payloads in switch policy

    IBM Champion
    Posted Mon February 14, 2022 04:51 PM
    You can refer to any context variable by simply using the variable name. 
    For example, if your data/element value is stored in a context variable called 'id', then you can use this condition like this : (id = '2')

          - switch:
              version: 2.0.0
              title: switch
              case:
                - condition: (id = '2')
                  execute:


    ------------------------------
    Romil Garg
    ------------------------------



  • 3.  RE: how to refer payloads in switch policy

    Posted Tue February 15, 2022 09:51 AM

    Hi Santhoshkumar,

    When you say "element in the request message" is your input XML or JSON?  "Element" to me says XML.  You'll first need to start off your assembly with a parse policy.  That will take the raw text in the request message and will parse it into either an XML DOM or JSON object depending upon what your payload is.  The parse of the request will be stored in the context "message" so then your switch condition would be

    message.body.parent.child.element = '1'

    That should work for JSON and for XML if your XML has no namespaces.  If your XML does have a namespace or if you need to test against an XML attribute, then you'll need to use the $xpath function, for example

    $xpath(message.body, '/*[@local-name() = 'parent']/*[@local-name() = 'child']/*[@local-name() = 'element'']) = 1

    Regards,



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



  • 4.  RE: how to refer payloads in switch policy

    Posted Tue February 15, 2022 10:27 AM
    thank you for details . but  we migrated apis from v5 to v10 by using AMU tool 10.0.3 -r2 version . the conditions in switch are not working . as they are getting converted to ($storageType(request.body.PolicyTransaction.ActionName) = "valuationAssignPolicyNumber"). do we need to rewrite it for all apis , if we have similar conditons.




    ------------------------------
    santhoshkumar surisetty
    ------------------------------



  • 5.  RE: how to refer payloads in switch policy

    Posted Wed February 16, 2022 08:33 AM

     Hi Santhoshkumar,

    $storageType per the API Connect documentation for jsonata functions of a switch should return one of   "binary" | "json" | "stream" | "xml".  See https://www.ibm.com/docs/en/api-connect/10.0.1.x?topic=switch-writing-condition-scripts-datapower-api-gateway.   I  don't see $storageType in the current AMU code base, but that's not to say it wasn't there in AMU tool 10.0.3 -r2 version and been removed with recent revisions.  What was your v5 switch condition that produced this resulting switch condition in the v10 api?  Investigation of this would be best if you were to open a PMR for this.  Please provide your v5 API yaml file and the resulting v10 API Gateway API yaml file with the issue and any AMU logs if you still have them.  As for your question "do we need to rewrite it for all APIs , if we have similar conditions." the AMU is a migration tool meant to be a one time usage tool.  Your options would be to fix any failing API or assuming this is a defect in the AMU, rerun the v5 to v10 migration when a fix is available.  I understand that isn't the most desired answer, hopefully what produced this is isolated to a particular type of v5 condition that doesn't impact a large number of APIs.


    Regards,



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



  • 6.  RE: how to refer payloads in switch policy

    Posted Wed February 16, 2022 11:12 AM
    Thank you Steve for Advise. i will open PMR regarding it

    ------------------------------
    santhoshkumar surisetty
    ------------------------------



  • 7.  RE: how to refer payloads in switch policy

    Posted Thu February 17, 2022 04:14 PM

    Thanks Santhoshkumar,

    I would be curious if you would post just the if/switch condition in your v5 switch that is being migrated to $storageType.  I could try a quick recreate in the AMU while waiting for the PMR.

    Regards,



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



  • 8.  RE: how to refer payloads in switch policy

    Posted Mon February 21, 2022 09:18 AM
    Hi Steve,


    sorry for the confusion . the storageType is added by us to verify what it returns as the request.body.requestHeader.echoback refrecnce is not working . we are still looking for the solution. 

    v5 condition
    =======


    v10 condition
    ============



    ------------------------------
    santhoshkumar surisetty
    ------------------------------



  • 9.  RE: how to refer payloads in switch policy

    Posted Mon February 21, 2022 10:02 AM
      |   view attached

    Hi Santhoshkumar,

    I believe the [-1] is your issue.  I also used $queryParameter instead of $urlParameter.  The purpose of the $urlParameter is to simply provide the value for the named parameter which could be either a query or path parameter, so it should still work, but $queryParameter only looks at query parameters.  I'll attach my simple test case yaml where I have a query parameter named "echo" and the switch is $queryParameter('echo') = "true" and it works as I'd expect.  The echo query parameter not provided or not "true" takes me to the otherwise clause, but "true" passes the condition.  The other part of your condition "request.body.requestHeader.echoBack" is also incorrect if you're trying to check for a request header in addition to the query parameters.  Use "request.headers.echoBack = "true" in that case.  My sample checks for both now and is also working $queryParameter('echo') = 'true' or request.headers.echo = 'true'

    Regards,
    Steve



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

    Attachment(s)



  • 10.  RE: how to refer payloads in switch policy

    Posted Tue February 22, 2022 09:37 AM
    Hi Steve ,

    Thank you for details .But  we have requestHeader in the payload which we are referring after invoke .we want to route message base on condition in switch . we are using request.body.requestHeader.echo ='true' which is not working and please tell me how to use function $storageType . please ignore urlparameter which is working fine.

    ------------------------------
    santhoshkumar surisetty
    ------------------------------



  • 11.  RE: how to refer payloads in switch policy

    Posted Tue February 22, 2022 11:08 AM

    Hi Santhoshkumar,

    I'm sorry, I didn't understand that the requestHeader was actually a json key in your JSON request payload.


    First, request.body is immutable and cannot be parsed.  It will always be a string/binary data.  You should have a parse policy at the beginning of your API which will take the copy of request.body, which is always copied to message.body at the start of your assembly processing, and will parse it into a JSON object, replacing message.body.  Then stepping through that object with message.body.requestHeader.echo in the switch condition should work.

    Regards,



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