Created
September 21, 2023 17:46
-
-
Save akanieski/53583358e97ad5bb739f5500cf8d63a4 to your computer and use it in GitHub Desktop.
http-request-loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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