Madhushree
Madhushree

Reputation: 11

KARATE API TESTING : not able to print response array which I am trying to get from If statement and wanted to pass it to next api call request url

step 1 : I am getting one variable in response of one API call which I stored in one variable

    * def ID = response.id

step 2 : In the second API call, response is the array of json object with multiple fields, after post method response I am trying to pass 'ID' inside if condition and based on if condition I am expecting array ( which is coming empty) example response of API [ {city: "NY", code:41049, id: ABC-123, name: "USA"},{city: "OT", code:42049, id: ABC-123, name: "CANADA"}

step 3: I want to pass step 2 printed array value of specific index in next API call urlpath (I am trying like)

And path dynamicform/'+ idarray +'/form1 ( getting null value here for +idarray+) when method get then status 200

Upvotes: 1

Views: 603

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58088

Please don't use JS loops. They get you into trouble, please refer to the docs for better approaches: https://github.com/karatelabs/karate#loops

Also JsonPath will give you what you need in 90% of the situations without needing loops. Please read the docs carefully and look for JsonPath.

* def ids = $[*].id
* match ids == ['ABC-123', 'ABC-123']

I also doubt whether concatenating ids into the path will work the way you expect. Please take the help of someone who knows JS if needed.

Maybe you are looking for this: https://github.com/karatelabs/karate#jsonpath-filters

* def fun = function(x){ return x.name == 'USA' } 
* def filtered = karate.filter(response, fun)
* def id = filtered[0].id

Upvotes: 0

Related Questions