https://github.com/chadnpc/poshrest

A module for working with HTTP RESTful APIs.

https://github.com/chadnpc/poshrest

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (9.5%) to scientific vocabulary

Keywords

http powershell rest-api
Last synced: 5 months ago · JSON representation

Repository

A module for working with HTTP RESTful APIs.

Basic Info
Statistics
  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
http powershell rest-api
Created 11 months ago · Last pushed 11 months ago
Metadata Files
Readme License

README.md

PoshRest

Build Status PowerShell Gallery Downloads

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

Installation

powershell Install-Module PoshRest -Scope CurrentUser

Basic Usage

```powershell Import-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)" } ```

Feature Examples

1. Chainable Configuration

powershell $client = [PoshRest]::new("https://api.restful-api.dev") .AddDefaultHeader("User-Agent", "MyApp/1.0") .AddDefaultParameter("api-version", "2.0", [ParameterType]::QueryString)

2. All Parameter Types

powershell $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})

3. XML/JSON Serialization

```powershell # 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}) ```

4. Cookie Management

powershell $client.AddCookie("session", "abc123", "api.restful-api.dev", "/api")

5. File Upload

powershell $request = [PoshRestRequest]::new("uploads", [HttpMethod]::Post) $request .AddFile("profile", "C:\profile.jpg") | .AddBody(@{userId=123})

6. Retry Policies

powershell $client.ConfigureRetry(3, [TimeSpan]::FromSeconds(1))

7. Caching

powershell $client.EnableCache() $client.Execute([PoshRestRequest]::new("cached-data", [HttpMethod]::Get))

8. Custom Authentication

powershell $client.SetAuthenticator({ param($req) $req.RequestMessage.Headers.Add("X-Dynamic-Header", (Get-Random)) })

9. URL Segments

powershell $request = [PoshRestRequest]::new("products/{category}/{id}", [HttpMethod]::Get) $request .AddParameter("category", "electronics", [ParameterType]::UrlSegment) .AddParameter("id", 789, [ParameterType]::UrlSegment)

10. Async Execution

powershell $client.ExecuteAsync($request) | Wait-Job | Receive-Job

Community

@GitHub Discussions are open for Feature requests & Troubleshooting help.

License

Released under the WTFPL License 🍷🗿.

Owner

  • Name: alain
  • Login: chadnpc
  • Kind: user

GitHub Events

Total
  • Push event: 6
  • Create event: 2
Last Year
  • Push event: 6
  • Create event: 2

Issues and Pull Requests

Last synced: 11 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Dependencies

.github/workflows/build_module.yaml actions
.github/workflows/codereview.yaml actions
  • anc95/ChatGPT-CodeReview v1.0.12 composite
.github/workflows/delete_old_workflow_runs.yaml actions
  • Mattraks/delete-workflow-runs v2 composite
.github/workflows/publish.yaml actions
  • actions/checkout v3 composite
  • actions/upload-artifact v3 composite