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.


#API Connect
#Applicationintegration
#APIConnect
 View Only
Expand all | Collapse all

How to validate duplicate field element in request body

  • 1.  How to validate duplicate field element in request body

    Posted Mon April 24, 2023 04:46 AM

    Hi,

    We want to validate duplicate filed element in request body that means when consumer access the API and send the request that time if they send duplicate filed element that time they showed the error.

    For ex. If account_no is one of the filed in request body and consumer send account_no in two times that time they show the error. That type of validation we required so please suggest us how we can implement that type of validation in API connect



    ------------------------------
    Jyoti Pednekar
    ------------------------------


  • 2.  RE: How to validate duplicate field element in request body

    Posted Fri May 05, 2023 09:12 AM

    Hi Jyoti,

    I'm assuming your request.body is a JSON payload and by sending a duplicate field element you're saying the body looks something like the following?

    {
      "account_no": "1234",
      "account_no": "5678",
      ... more fields
    }

    Which gateway are you using, a v5 compatible gateway or a v10 API Gateway?

    For a v5 compatible gateway, the INPUT context to DataPower is parsed and placed into request.body.  The parsing in DataPower only allows one key of the same name at each level of the object hierarchy, and I believe the last key will win, so request.body will only show you only the last of the duplicate keys.  The INPUT context of DataPower will remain a string, so the only advice I can provide would be use a GatewayScript policy to do a session.INPUT.readAsBuffer to read the INPUT context,  convert that buffer to a string using the toString() function, and then use string manipulation to see if you have duplicates.  If a duplicate is found you should do an apim.error function to stop the assembly and start error processing.

    For the v10 API Gateway, request.body is immutable but unlike v5c it is also streamed, meaning that you cannot see request.body in the API Gateway context unless and until you

    1. Do an initial parse policy where the parsing will read everything off the stream to place the parsed JSON object into message.body.  You can then in a GatewayScript do a context.get('request.body') which would have the now buffered request. Note that the parsed object in message.body would have eliminated the duplicate key just as v5c would have, again the last key would be the one you'd see there.

    2. A GatewayScript policy that will do a context.getMessage('request').body.readAsBuffer which will also read from the stream and provide the buffer to the asynchronous function once the data is read.

    In either case, once you have the buffer, again convert the buffer to a string and use string manipulation to see if you have duplicates and you would do a context.reject if you find a duplicate to stop the assembly.

    Best Regards,
    Steve



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