Skip to content

Instantly share code, notes, and snippets.

@akanieski
Created September 21, 2023 17:46
Show Gist options
  • Save akanieski/53583358e97ad5bb739f5500cf8d63a4 to your computer and use it in GitHub Desktop.
Save akanieski/53583358e97ad5bb739f5500cf8d63a4 to your computer and use it in GitHub Desktop.
http-request-loop
# Define your array of strings
$strings = @("https://example.com/api/endpoint1", "https://example.com/api/endpoint2", "https://example.com/api/endpoint3")
# Specify the CSV file path
$csvFilePath = "responses.csv"
# Initialize an empty array to store the response data
$responseData = @()
# Iterate over each string in the array
foreach ($url in $strings) {
try {
# Send an HTTP GET request to the URL
$response = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json"
# Append the JSON response to the array
$responseData += $response
}
catch {
Write-Host "Error: $_"
}
}
# Export the response data to a CSV file
if ($responseData.Count -gt 0) {
$responseData | Export-Csv -Path $csvFilePath -NoTypeInformation -Append
Write-Host "Responses appended to $csvFilePath"
}
else {
Write-Host "No responses to append."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment