Skip to content

JayTwoLab/rest_powershell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

rest_powershell


Introduction

  • rest api for powershell script

Usage

────────────────────────────────────────────────────────────
2) Usage Examples (Postman Alternative)
────────────────────────────────────────────────────────────

[GET]
.\rest.ps1 -Url "https://httpbin.org/get" -Query @{ q="test" } -ForceTls12

[POST JSON]
.\rest.ps1 -Url "https://httpbin.org/post" -Method POST `
  -Headers @{ Accept="application/json" } `
  -Body '{"name":"jaytwo"}' -ForceTls12

[POST FORM]
.\rest.ps1 -Url "https://httpbin.org/post" -Method POST `
  -Body @{ id=1; name="jaytwo" } -ForceTls12

[Maintain Cookies/Session]
.\rest.ps1 -Url "https://httpbin.org/cookies/set?x=1" -UseSession -ForceTls12
.\rest.ps1 -Url "https://httpbin.org/cookies" -UseSession -ForceTls12

────────────────────────────────────────────────────────────
3) POST -> Receiving JSON / XML Response Examples
────────────────────────────────────────────────────────────

[POST -> JSON Response]
.\rest.ps1 -Url "https://httpbin.org/post" -Method POST `
  -Headers @{ Accept="application/json" } `
  -Body '{"id":1,"name":"jaytwo"}' -ForceTls12

[POST -> XML Response]
.\rest.ps1 -Url "https://httpbin.org/post" -Method POST `
  -Headers @{
    Accept="application/xml"
    Content-Type="application/xml"
  } `
  -Body '<user><id>1</id><name>jaytwo</name></user>' -ForceTls12

[Parsing XML]
[xml]$xml = Get-Content response.xml
$xml.slideshow.title

[Parsing JSON]
$json = Get-Content response.json | ConvertFrom-Json
$json.slideshow.title

────────────────────────────────────────────────────────────
4) POST Request & JSON / XML Output Examples
────────────────────────────────────────────────────────────

[7] POST -> Receiving JSON Response (Pretty JSON Output)
--------------------------------
# When the server returns a JSON response
# (httpbin.org echoes the request content back as JSON)

.\rest.ps1 `
  -Url "https://httpbin.org/post" `
  -Method POST `
  -Headers @{
    "Accept" = "application/json"
    "Content-Type" = "application/json"
  } `
  -Body '{
    "id": 1001,
    "name": "jaytwo",
    "active": true
  }' `
  -ForceTls12

# Result:
# - StatusCode / Headers Output
# - If the Body is JSON, it is automatically pretty-printed.


[8] POST -> Receiving XML Response (Raw Output)
--------------------------------
# When the server returns XML
# (Requesting XML via Accept Header)

.\rest.ps1 `
  -Url "https://httpbin.org/post" `
  -Method POST `
  -Headers @{
    "Accept" = "application/xml"
    "Content-Type" = "application/xml"
  } `
  -Body '<user>
    <id>1001</id>
    <name>jaytwo</name>
    <active>true</active>
  </user>' `
  -ForceTls12

# Result:
# - XML is not target for Pretty-Json, so it is output as raw text.
# - Can be cast to [xml] for further processing if needed.


[9] Parsing XML Response to PowerShell Object (Reference)
--------------------------------
# Save XML response to a file and parse in PowerShell

.\rest.ps1 `
  -Url "https://httpbin.org/xml" `
  -Method GET `
  -OutFile ".\response.xml" `
  -ForceTls12

# XML Parsing
[xml]$xml = Get-Content .\response.xml
$xml.slideshow.title
$xml.slideshow.slide.title


[10] Parsing JSON Response to PowerShell Object (Reference)
--------------------------------
# Save JSON Response to a file

.\rest.ps1 `
  -Url "https://httpbin.org/json" `
  -Method GET `
  -OutFile ".\response.json" `
  -ForceTls12

# JSON Parsing
$json = Get-Content .\response.json | ConvertFrom-Json
$json.slideshow.title
$json.slideshow.slides[0].title

About

rest api for powershell

Resources

License

Stars

Watchers

Forks

Packages

No packages published