Search code examples
arraysapicucumberkarate

How to loop SOAP request with values received from a response array


I am making a SOAP request, and I am receiving the response that's returned as an array:

 - [print] [
  "M4205N",
  "M4206U"
]

For each item in the array, I want to make another SOAP request. I've read how you can do this with tables and call a feature file, and I've read how to loop through an array, and call a js function. I cannot figure out how to loop through the array, and pass each value to another SOAP request XML (one at a time).

I want to do something like this:

  • Given soapURL
  • And method post
  • def responseArray = /xml path for the codes I want/
  • def result = call read('otherRequest.feature') responseArray

The otherRequest.feature file would look something like this:

@ignore
Feature:

Background:
* def myNewRequest = read('soap.xml')

Scenario:
  Given soapURL
  * replace myNewRequest 
  | token      | value              |
  | @@refNum@@ | responseArrayValue |
  When request myNewRequest 
  And method post

However, I get this error:

GetNewMessageList.feature:27 - argument not json or map for feature call loop array position: 0, M4205N

How can I loop through each item in the array, and pass each value to the other feature file?


Solution

  • The addition of this one line should do what you want. Yes there is a hard requirement that the "loop" array should be an array of JSON objects. But you can convert an array of primitives in one step:

    * def responseArray = karate.mapWithKey(responseArray, 'responseArrayValue')
    

    This is documented here: https://github.com/intuit/karate#json-transforms