How to append web request results in a while loop

时间:2019-04-16 22:51:39

标签: powershell webrequest

I need to call an API and loop through the various pages of results that are returned and append them all to one object.

I've tried the code below. Generally += works when appending to a powershell object, but no luck this time.

Note: URI and Get are both functions that are defined elsewhere. They work as expected elsewhere in the code.

$min=1
$max=2

while ($min -le $max){
    $url= URI "tasks?page=$min"
    $x=Get $url
    if($min=1){
        $response=$x
    }
    else{
        $response+=$x
    }

    $min=$min+1
}

sample response (converted to json):

  "value": [
    {
      "task_id": 17709655,
      "project_id": 1928619,
      "start_date": "2019-04-11",
      "end_date": "2019-11-29",
      "start_time": null,
      "hours": 1.5,
      "people_id": 17083963,
      "status": 2,
      "priority": 0,
      "name": "",
      "notes": "",
      "repeat_state": 0,
      "repeat_end_date": null,
      "created_by": 331791,
      "modified_by": 0,
      "created": "2019-04-12 00:39:30.162",
      "modified": "2019-04-12 00:39:30.162",
      "ext_calendar_id": null,
      "ext_calendar_event_id": null,
      "ext_calendar_recur_id": null
    },
    {
      "task_id": 17697564,
      "project_id": 1928613,
      "start_date": "2019-10-08",
      "end_date": "2019-10-08",
      "start_time": null,
      "hours": 8,
      "people_id": 17083966,
      "status": 2,
      "priority": 0,
      "name": "",
      "notes": "",
      "repeat_state": 0,
      "repeat_end_date": null,
      "created_by": 327507,
      "modified_by": 0,
      "created": "2019-04-11 16:10:22.969",
      "modified": "2019-04-11 16:10:22.969",
      "ext_calendar_id": null,
      "ext_calendar_event_id": null,
      "ext_calendar_recur_id": null
    }
  ],
  "Count": 2
}```

1 个答案:

答案 0 :(得分:0)

Assuming you want the output to be an array, I'd write your code like this:

$min=1
$max=2

$response = foreach ($Page in $min..$max) {
    $url = URI "tasks?page=$Page"
    Get $url
}

This is the generally preferred method, because both Strings and Arrays have fixed lengths in .Net and therefore PowerShell.

Here, $response[0] should be the first response, $response[1] the second, etc.

If the above doesn't work for you, then my first guess would be that the output of Get isn't a string.

If you're expecting $response to be a single valid JSON string containing all the responses, then my response is "JSON doesn't work that way." You'll have to parse each JSON response to objects (hint: ConvertFrom-Json) combine them, and then possibly convert them back to JSON (ConvertTo-Json). Note that .Net's native dialect of JSON doesn't match the rest of the Internet's dialect of JSON, particularly with dates (though it looks like your dates here are strings). You may want to use JSON.Net, which I believe does match the common Internet dialect.

You may be able to combine $response like this:

$CombinedResponse = '[' + ($response -join ',') + ']'

But I don't know how well that's going to work if you then try to parse that as JSON.