A lightweight PowerShell module for working with HTTP RESTful APIs.
If you want advanced features beyond basic Invoke-RestMethod while maintaining PowerShell syntax, then this module is for you.
Stuff like:
- Caching: GET request caching
- Serialization: JSON/XML with custom options
- Retry Policies: Automatic retries with backoff strategies
- Custom Auth: Scriptblock-based authentication handlers
- File Uploads : MultipartFormData with files
Install-Module PoshRest -Scope CurrentUserImport-Module PoshRest
# Create client with base URL
$client = [PoshRest]::new("https://api.restful-api.dev")
# Configure defaults
$client.AddDefaultHeader("X-API-Key", "your-api-key").SetAuthentication("Bearer", "your-token").ConfigureRetry(5, [TimeSpan]::FromSeconds(2)).EnableCache()
# Create request with URL segment
$request = [PoshRestRequest]::new("users/{id}", [HttpMethod]::Get)
$request
.AddParameter("id", 123, [ParameterType]::UrlSegment)
.AddHeader("Accept", "application/json")
# Execute synchronously
$response = $client.Execute($request)
if ($response.IsSuccessful) {
$user = $response.Content | ConvertFrom-Json
Write-Host "User: $($user.name)"
} else {
Write-Error "Request failed: $($response.StatusCode)"
}$client = [PoshRest]::new("https://api.restful-api.dev")
.AddDefaultHeader("User-Agent", "MyApp/1.0")
.AddDefaultParameter("api-version", "2.0", [ParameterType]::QueryString)$request = [PoshRestRequest]::new("data", [HttpMethod]::Post)
$request
.AddParameter("page", 2, [ParameterType]::QueryString)
.AddParameter("auth", "token", [ParameterType]::Header)
.AddParameter("userId", 456, [ParameterType]::UrlSegment)
.AddParameter("rememberMe", $true, [ParameterType]::Cookie)
.AddBody(@{name="John"; age=30})# JSON with custom options
$client.JsonOptions.PropertyNamingPolicy = [JsonNamingPolicy]::SnakeCase
# XML with namespaces
$client.ConfigureXml(
[XmlSerializerNamespaces]::new(@([XmlQualifiedName]::new("ns", "http://example.com"))),
[XmlWriterSettings]::new() | Add-Member -MemberType NoteProperty -Name Indent -Value $true
)
$request.AddXmlBody([PSCustomObject]@{Name="John"; Age=30})$client.AddCookie("session", "abc123", "api.restful-api.dev", "/api")$request = [PoshRestRequest]::new("uploads", [HttpMethod]::Post)
$request
.AddFile("profile", "C:\profile.jpg") |
.AddBody(@{userId=123})$client.ConfigureRetry(3, [TimeSpan]::FromSeconds(1))$client.EnableCache()
$client.Execute([PoshRestRequest]::new("cached-data", [HttpMethod]::Get))$client.SetAuthenticator({
param($req)
$req.RequestMessage.Headers.Add("X-Dynamic-Header", (Get-Random))
})$request = [PoshRestRequest]::new("products/{category}/{id}", [HttpMethod]::Get)
$request
.AddParameter("category", "electronics", [ParameterType]::UrlSegment)
.AddParameter("id", 789, [ParameterType]::UrlSegment)$client.ExecuteAsync($request) | Wait-Job | Receive-Job@GitHub Discussions are open for Feature requests & Troubleshooting help.
Released under the WTFPL License 🍷🗿.