Layer7 API Management

  • 1.  Question with Run Assertions for Each Item Assertion

    Broadcom Employee
    Posted Oct 01, 2017 07:51 AM

    Hi,

     

    In the docops example for this assertion, a sample policy is given with a POST request to 2 different URLs in the child assertions for each iteration of the multi valued variable. How do we use this assertion effectively in a scenario where we need to have a GET request to an URL in the child assertions since the response generated in each iteration of the child assertion will be over written by the response of the next iteration. If we need to store the response of route assertion in each iteration of the child assertion and then need to combine the responses of all iterations to return back to the client, how do we best achieve it with the help of this assertion?

     

    Requirement example:

     

    - Request might contain 10 or more Part IDs. This can be stored to a multi valued context variable (Number of Part IDs in the request is not fixed with no maximum limit)

    - For each Part ID that comes in the request, we need to call a back-end API and get the part details

    - We need to combine the part details responses for each Part ID (from each iteration of Part ID) and build a single JSON document as response to the client.

     

    How can we use the "Run Assertion for Each Item" assertion to execute this loop without responses of each iteration getting overwritten? Or is there any simpler way to achieve this loop without the help of this assertion?

     

    Regards,

    Karthik



  • 2.  Re: Question with Run Assertions for Each Item Assertion
    Best Answer

    Posted Oct 02, 2017 03:19 PM

    Untested, but I assume you could use a Set Context Variable assertion (i.e ${my_final_response}) to set a variable to an empty string before entering the For Each, and then inside each loop, extract the response part you need to append (i.e. in ${my_response_part}, and then append it to the previously set context variable like so: ${my_final_response}${my_response_part}

     

    After the For Each, use the Return Template Response To Requestor assertion to return the final response back and put the value of ${my_final_response} as the body.



  • 3.  Re: Question with Run Assertions for Each Item Assertion

    Broadcom Employee
    Posted Oct 03, 2017 11:29 AM

    Great. This looks like it will solve the problem. However, I will test it in one of my scenarios and update as Answered. Thank you very much!