The issue you're encountering is due to how Karate handles dynamic key-value pairs in JSON objects, particularly when you want to use a variable for the key inside a match statement.
When you want to match a response where the key is dynamically generated from a variable, you need to use the correct syntax for dynamic key-value pairs in JSON. Specifically, Karate supports dynamic keys using the following syntax.
{ (variableName): value }
This allows the key to be determined at runtime, and the variable will be resolved to its actual value.
Corrected Example: In your original code:
Given path 'path'
When method get
And def var1 = "name"
Then match response == """
{ #(var1): "val1" }
"""
The #(var1) inside the multi-line string doesn't get resolved in this context. The proper way to achieve this dynamic key matching is as follows:
Given path 'path'
When method get
And def var1 = "name"
Then match response == { (var1): "val1" }
Thank you.