Request and Response in Kurtosis #684
-
I need to make a request to a service, and wait until an array in the response body has a nonzero length. How can I do this in Kurtosis? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This can be done via the The way E.g. if you define a recipe with an recipe = GetHttpRequestRecipe(
port_id = "my_port",
endpoint = "/some-endpoint",
extract = {
# Uses jq to extract the value of the key
"valueOfMyKey" : ".myKey",
},
)
response = plan.request(service = "my-service", recipe = recipe)
# Contains a future reference to the value of "myKey"
plan.print(response["extract.valueOfMyKey"]) So in your case, to wait until the length of an array is non-zero, let's suppose that your HTTP request is returning a body like this: {
"theArray": [],
} We can get the length in recipe = recipe = GetHttpRequestRecipe(
port_id = "my_port",
endpoint = "/some-endpoint",
extract = {
# Leverage jq's length function
"arrayLen" : ".theArray | length",
},
)
final_response = plan.wait(
service_name = "my-service",
recipe = recipe,
field = "extract.arrayLen",
assertion = ">",
target_value = 0,
)
plan.print("The array length after passing the wait is: " + final_response["extract.arrayLen"]) |
Beta Was this translation helpful? Give feedback.
This can be done via the
extract
field that's available on GetHttpRequestRecipe, PostHttpRequestRecipe, and ExecRecipe in combination with plan.wait.The way
extract
works is that, when the Recipe is run (via plan.request for Get/PostHttpRecipe and plan.exec for ExecRecipe), the result object will be a dictionary containing future references to the extracted values.E.g. if you define a recipe with an
extract
ofvalueOfMyKey
,response["extract.valueOfMyKey"]
will be a future to that value at execution time: