Calling Web APIs with Powershell
You go along way by calling apis at the command line. Powershell provides the following methods that you will find useful...
Examples of simple api web request
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
# Get a page over http | |
Invoke-RestMethod -URI https://www.google.com -Method "GET" | |
# Post text content over http | |
Invoke-RestMethod -URI http://mvcsampleapp.apphb.com/Feedback -Body "Email=afsd&FirstName=sdfasdf&Surname=fasdfsd&Text=fasdfsdafsd" -ContentType "text/html" -Method POST | |
# Get a page over http specifying the IE proxey server | |
$Webproxy = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer | |
Invoke-RestMethod -URI https://www.google.com -Method "GET" -Proxy $Webproxy -UseDefaultCredentials |
Examples using the IE Browser Proxey settings
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
# Read out proxy settings | |
$Webproxy = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer | |
# Wrapper method for Invokeing a rest method | |
function Invoke-Request { | |
Param( [string]$Method="GET", [string]$Body, [string]$Url ) | |
if($Method -eq "GET") { | |
return Invoke-RestMethod -Proxy $Webproxy -UseDefaultCredentials -URI $Url -Method "GET" | |
} else { | |
return Invoke-RestMethod -Proxy $Webproxy -UseDefaultCredentials -URI $Url -ContentType "application/json" -Method $Method -Body $Body | |
} | |
} | |
#Example Usage Get | |
Invoke-Rest -Url "https://octopus2/api//Teams?apiKey=$API_KEY" |