API Connect

 View Only
  • 1.  Parse policy- Duplicate Key in JSON Request

    Posted Sun September 04, 2022 10:26 AM
    We have an API configured with Parse and Validate policy.
    As part of testing an error case, We are getting  JSON request with Duplicate key and backend should throw an error. But the duplicate keys have been removed at Parsing policy and sending the request to the backend and getting success response. How to process the Actual request to the backend?

    FICode is the duplicate key:
    Request which is going to backend after Parsing, We can see there was only one FICode:

    ------------------------------
    Ravikumar Kunibilli
    ravikumarkunibilli@gmail.com
    ------------------------------


  • 2.  RE: Parse policy- Duplicate Key in JSON Request

    Posted Mon September 05, 2022 03:15 AM
    Hello All,

    Can anyone assist me on above posted scenario.
    My API flow will be like below, I can't remove Parse as we configured Validate policy for the validation. Parse policy is removing the duplicate keys and processing the request hence we are getting success response instead of error response from the backend.

    How can we handle duplicate keys in API Connect to pass the request to the backend as it is.

                                                                                 Parse--Validate--Invoke​

    ------------------------------
    Ravikumar Kunibilli
    ravikumarkunibilli@gmail.com
    ------------------------------



  • 3.  RE: Parse policy- Duplicate Key in JSON Request

    Posted Mon September 05, 2022 05:03 AM
    @Romil Garg

    Can you provide your thoughts on above issue.


    ------------------------------
    Ravikumar Kunibilli
    ravikumarkunibilli@gmail.com
    ------------------------------



  • 4.  RE: Parse policy- Duplicate Key in JSON Request

    Posted Mon September 12, 2022 09:49 AM
    Hi Ravi,
    You can convert the json object into a string, then do string splitting on the object by using the name of the duplicate object as an input. If the resultant array contains more than two elements then throw an error else continue the parsing. PFB the ESQL code to implement this functionality.

    CALL Split(CAST(ASBITSTREAM(InputRoot.HTTPInputHeader CCSID 1208) AS CHARACTER CCSID 1208), OutputLocalEnvironment, 'duplicateKey');

    CREATE PROCEDURE Split (IN S CHARACTER, IN Env REFERENCE, IN Delim CHARACTER)
    BEGIN
    DECLARE P INTEGER;
    DECLARE Idx INTEGER 1;

    SET Env.Split = NULL;

    REPEAT
    SET P = POSITION(Delim IN S);
    IF P = 0 THEN
    SET Env.Split.Array[Idx] = S;
    ELSE
    SET Env.Split.Array[Idx] = LEFT(S, P - 1);
    SET S = SUBSTRING(S FROM P + LENGTH(Delim));
    SET Idx = Idx + 1;
    END IF;
    UNTIL P = 0
    END REPEAT;
    END;

    ------------------------------
    Ali Mazhar
    ------------------------------



  • 5.  RE: Parse policy- Duplicate Key in JSON Request

    Posted Tue September 13, 2022 02:57 AM
    Hi @Ali Mazhar

    Thanks for sharing your comments. Here in API Connect, I can use only Gateway script or XSLT, As we are getting JSON request we should use Gateway script. Do yo have any Gateway script code to implement the same functionality which you have implemented in ESQL.

    Thanks,
    Ravi


    ------------------------------
    Ravikumar Kunibilli
    ravikumarkunibilli@gmail.com
    ------------------------------



  • 6.  RE: Parse policy- Duplicate Key in JSON Request

    Posted Wed October 05, 2022 09:18 AM

    Hi Ravikumar,

    Since you're using a Parse policy you're using a v10 API Gateway service.   A JSON parse in DataPower, either the parse policy or a GatewayScript JSON.parse function, will remove duplicate keys I believe with the last one being utilized, so your only recourse is to have a GatewayScript that would read the unparsed request.body as a buffer and do your own parsing, looking for duplicate keys at any object level, and reject the request if you find one.  If the request passes you could then do the parse policy to then do the validation. It would be an interesting JavaScript exercise, probably a recursive function that you'd need to match up your curly braces to get an object and once you would have a node object, you could split that on commas to get each name:value  pair and look for duplicate names. Note that if you want to read request.body you should enable buffering on the API as well, otherwise request.body would not be accessible to you.

    Best Regards,

    Steve



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