|
| 1 | +#################################### |
| 2 | +# Invoke-HttpRequestWithRetry.ps1 # |
| 3 | +#################################### |
| 4 | +# Version: 0.1.0 |
| 5 | + |
| 6 | +<# |
| 7 | +.SYNOPSIS |
| 8 | +Invokes an HTTP request with automatic retry logic for transient errors. |
| 9 | +
|
| 10 | +.DESCRIPTION |
| 11 | +Makes HTTP requests using Invoke-WebRequest or Invoke-RestMethod with |
| 12 | +automatic retry for transient HTTP errors (408, 429, 500, 502, 503, 504). |
| 13 | +
|
| 14 | +.PARAMETER Uri |
| 15 | +The URI to send the request to. |
| 16 | +
|
| 17 | +.PARAMETER Method |
| 18 | +The HTTP method for the request. Defaults to GET. |
| 19 | +
|
| 20 | +.PARAMETER MaxRetryCount |
| 21 | +Maximum number of retries for transient errors. Defaults to 10. |
| 22 | +
|
| 23 | +.PARAMETER RetryIntervalSeconds |
| 24 | +Seconds to wait between retries. Defaults to 3. |
| 25 | +
|
| 26 | +.PARAMETER OutFile |
| 27 | +If specified, downloads the response to this file path using Invoke-WebRequest. |
| 28 | +
|
| 29 | +.PARAMETER SkipHttpErrorCheck |
| 30 | +If specified, does not throw on HTTP error status codes. |
| 31 | +Returns the response object without error. |
| 32 | +
|
| 33 | +.PARAMETER TimeoutSec |
| 34 | +Timeout in seconds for the HTTP request. If not specified, uses the default. |
| 35 | +
|
| 36 | +.PARAMETER Body |
| 37 | +The body of the request. |
| 38 | +
|
| 39 | +.PARAMETER ContentType |
| 40 | +The content type of the request body. |
| 41 | +
|
| 42 | +.PARAMETER Headers |
| 43 | +Additional headers to include in the request. |
| 44 | +
|
| 45 | +.PARAMETER ReturnStatusCode |
| 46 | +If specified alongside SkipHttpErrorCheck, returns a hashtable with |
| 47 | +Result and StatusCode properties (similar to Invoke-GitHubApiRequest). |
| 48 | +
|
| 49 | +.EXAMPLE |
| 50 | +Invoke-HttpRequestWithRetry -Uri "https://api.releases.hashicorp.com/v1/releases/terraform?limit=20" |
| 51 | +
|
| 52 | +.EXAMPLE |
| 53 | +Invoke-HttpRequestWithRetry -Uri "https://example.com/file.zip" -OutFile "./file.zip" |
| 54 | +
|
| 55 | +.EXAMPLE |
| 56 | +Invoke-HttpRequestWithRetry -Uri "https://example.com" -Method Head -SkipHttpErrorCheck -MaxRetryCount 0 |
| 57 | +
|
| 58 | +.NOTES |
| 59 | +# Release notes 25/03/2026 - V0.1.0: |
| 60 | +- Initial release. |
| 61 | +#> |
| 62 | + |
| 63 | +function Invoke-HttpRequestWithRetry { |
| 64 | + [CmdletBinding()] |
| 65 | + param ( |
| 66 | + [Parameter(Mandatory = $true, Position = 1, HelpMessage = "The URI to send the request to.")] |
| 67 | + [string] $Uri, |
| 68 | + |
| 69 | + [Parameter(Mandatory = $false, HelpMessage = "The HTTP method for the request.")] |
| 70 | + [string] $Method = "GET", |
| 71 | + |
| 72 | + [Parameter(Mandatory = $false, HelpMessage = "Maximum number of retries for transient errors.")] |
| 73 | + [int] $MaxRetryCount = 10, |
| 74 | + |
| 75 | + [Parameter(Mandatory = $false, HelpMessage = "Seconds to wait between retries.")] |
| 76 | + [int] $RetryIntervalSeconds = 3, |
| 77 | + |
| 78 | + [Parameter(Mandatory = $false, HelpMessage = "If specified, downloads the response to this file path.")] |
| 79 | + [string] $OutFile, |
| 80 | + |
| 81 | + [Parameter(Mandatory = $false, HelpMessage = "If specified, does not throw on HTTP error status codes.")] |
| 82 | + [switch] $SkipHttpErrorCheck, |
| 83 | + |
| 84 | + [Parameter(Mandatory = $false, HelpMessage = "Timeout in seconds for the HTTP request.")] |
| 85 | + [int] $TimeoutSec, |
| 86 | + |
| 87 | + [Parameter(Mandatory = $false, HelpMessage = "The body of the request.")] |
| 88 | + [object] $Body, |
| 89 | + |
| 90 | + [Parameter(Mandatory = $false, HelpMessage = "The content type of the request body.")] |
| 91 | + [string] $ContentType, |
| 92 | + |
| 93 | + [Parameter(Mandatory = $false, HelpMessage = "Additional headers to include in the request.")] |
| 94 | + [hashtable] $Headers, |
| 95 | + |
| 96 | + [Parameter(Mandatory = $false, HelpMessage = "If specified, returns a hashtable with Result and StatusCode.")] |
| 97 | + [switch] $ReturnStatusCode |
| 98 | + ) |
| 99 | + |
| 100 | + $isDownload = -not [string]::IsNullOrEmpty($OutFile) |
| 101 | + $transientStatusCodes = @(408, 429, 500, 502, 503, 504) |
| 102 | + $maxAttempts = $MaxRetryCount + 1 |
| 103 | + |
| 104 | + # Build common parameters |
| 105 | + $commonParams = @{ |
| 106 | + Uri = $Uri |
| 107 | + Method = $Method |
| 108 | + ErrorAction = "Stop" |
| 109 | + } |
| 110 | + |
| 111 | + if ($PSBoundParameters.ContainsKey("TimeoutSec")) { |
| 112 | + $commonParams["TimeoutSec"] = $TimeoutSec |
| 113 | + } |
| 114 | + |
| 115 | + if ($PSBoundParameters.ContainsKey("Body")) { |
| 116 | + $commonParams["Body"] = $Body |
| 117 | + } |
| 118 | + |
| 119 | + if ($PSBoundParameters.ContainsKey("ContentType")) { |
| 120 | + $commonParams["ContentType"] = $ContentType |
| 121 | + } |
| 122 | + |
| 123 | + if ($PSBoundParameters.ContainsKey("Headers")) { |
| 124 | + $commonParams["Headers"] = $Headers |
| 125 | + } |
| 126 | + |
| 127 | + for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) { |
| 128 | + try { |
| 129 | + if ($isDownload) { |
| 130 | + Invoke-WebRequest @commonParams -OutFile $OutFile |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + if ($SkipHttpErrorCheck) { |
| 135 | + $response = Invoke-WebRequest @commonParams -SkipHttpErrorCheck -UseBasicParsing |
| 136 | + |
| 137 | + $code = [int]$response.StatusCode |
| 138 | + |
| 139 | + if ($code -in $transientStatusCodes -and $attempt -lt $maxAttempts) { |
| 140 | + Write-Warning "Request to $Uri returned status $code (attempt $attempt of $maxAttempts). Retrying in $RetryIntervalSeconds seconds..." |
| 141 | + Start-Sleep -Seconds $RetryIntervalSeconds |
| 142 | + continue |
| 143 | + } |
| 144 | + |
| 145 | + if ($ReturnStatusCode) { |
| 146 | + return @{ |
| 147 | + Result = $response |
| 148 | + StatusCode = $code |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return $response |
| 153 | + } |
| 154 | + |
| 155 | + return (Invoke-WebRequest @commonParams -UseBasicParsing) |
| 156 | + } catch { |
| 157 | + $responseCode = $null |
| 158 | + if ($_.Exception.Response) { |
| 159 | + $responseCode = [int]$_.Exception.Response.StatusCode |
| 160 | + } |
| 161 | + |
| 162 | + $isTransient = $responseCode -in $transientStatusCodes |
| 163 | + |
| 164 | + if ($isTransient -and $attempt -lt $maxAttempts) { |
| 165 | + Write-Warning "Request to $Uri failed with status $responseCode (attempt $attempt of $maxAttempts). Retrying in $RetryIntervalSeconds seconds..." |
| 166 | + Start-Sleep -Seconds $RetryIntervalSeconds |
| 167 | + } else { |
| 168 | + throw |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments