How to enforce all JSON values of a request payload at once.

masdevalmasdeval Posts: 3

Hi guys.

I want to create a responder that will fail if any of the fields or values that come in a request is different from what was defined as the Request Template. How can I achieve this?

I have been trying using HTTP Simple Mock and JSON Assertors but I couldn`t get this behavior yet.

Best Answer

  • OmarROmarR Posts: 227 admin
    Accepted Answer

    An alternative would be to use scripting in custom correlation and compare the entire request payload as a single string.

    Below is an example (Right-click>View Image):

    import com.parasoft.api.CorrelationScriptingHookConstants
    def correlateOperation(context) {
        requestBodyStr = context.get(CorrelationScriptingHookConstants.MESSAGE_STR)     // String
        str1=requestBodyStr.replaceAll("[\\s]","")
        return str1.contains('{"glossary":{"title":"exampleglossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"StandardGeneralizedMarkupLanguage","Acronym":"SGML","Abbrev":"ISO8879:1986","GlossDef":{"para":"Ameta-markuplanguage,usedtocreatemarkuplanguagessuchasDocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}')
    }
    

    For simplicity, I remove all white space characters using regex before comparison. Give this a try.

Answers

  • OmarROmarR Posts: 227 admin

    How big is the request payload?
    One way to do this is to use responder correlation or data source correlation and add each field individually to request body correlation. However, this may be tedious for larger payloads.

  • masdevalmasdeval Posts: 3

    Hi Omar. This is the point, the payload is big. Configuring individually is exactly what I am trying to avoid. I would expect that what I need would be a common requirement but still did not find a solution. Any other lead?

  • masdevalmasdeval Posts: 3

    Yeah, it worked! Thank you.

Sign In or Register to comment.