Peter
Peter

Reputation: 1894

Use each element of var to make a http request in Mule

I have a collection of matches saved to a var. Each match has a gameId. I want to use this gameId to make an API call.
I set up a for each scope using matches with a counter var.

<set-variable value="#[payload]" doc:name="Set Matches" doc:id="bb1093b0-3dd9-4515-8f42-dd496f00477b" variableName="matches"/>
<set-variable value="#[[]]" doc:name="Deaths" doc:id="a7341e19-07f8-418d-b39a-5bc415e75fba" variableName="deaths"/>
<foreach doc:name="For each match" doc:id="506fa944-d46f-4684-9c75-73088265ca80" collection="vars.matches" rootMessageVariableName="matches">
    <http:request method="GET" doc:name="GetTimeline" doc:id="63708489-4d10-4e79-b5aa-c8fc723e80c6" config-ref="HTTP_Request_configuration" path="/match/v4/timelines/by-match/{matchId}">
        <http:uri-params ><![CDATA[#[output application/java
---
{
    "matchId" : vars.matches[vars.counter + 1].gameId
}]]]></http:uri-params>
    </http:request>
    <ee:transform doc:name="MapDeaths" doc:id="a6e22968-2229-4574-8cae-44225013b9cf">
        <ee:message>
            <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
    frames: payload.frames map ( frame , indexOfFrame ) -> {
        events: frame.events map ( event , indexOfEvent ) -> vars.deaths + event
    }
}]]></ee:set-payload>
        </ee:message>
    </ee:transform>
</foreach>

Yet matchId from "matchId" : vars.matches[vars.counter + 1].gameId always evalutes to null. What am I missing there?
Edit:
I'm using Mule 4.2 and the json structure of matches looks like this:

[
  {
    "matches": [
      {
        "key": "val",
        "key": val,
        ...
      },
      {
        "key": "val",
        "key": val,
        ...
      },
      ...

Upvotes: 0

Views: 465

Answers (2)

aled
aled

Reputation: 25664

The problem seems to be that your payload and the expression you are using doesn't match (pun not intended). The payload is an array that contains objects, with a matches attribute. The expression apparently assumes that the payload contains a matches attribute, which contains a gameId attribute. That is the reason it returns null.

Also the intention is not clear, given that you didn't show were gameId is in your example.

Let's try with a more detailed example.

For the input:

[
  {
    "matches": [
      {
        "keyA": "val1",
        "keyB": "val2"
      },
      {
        "keyC": "val3",
        "keyD": "val4"
      }
    ]
  }
]

The expression payload.matches.keyA returns null.

You could do payload[0].matches[0].keyA to obtain "val1".

This assumes there data is from the first element of the array and you want to use only one key from one of the matches. Of course if you need to iterate on the different arrays levels you will need to use map or group, but it is a different use case.

Upvotes: 1

Ven
Ven

Reputation: 2014

Set {matchId} as Query string parameter and define request a Target variable, use that in dataweave

enter image description here

Upvotes: 1

Related Questions