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


# 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


# 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"




Popular posts from this blog

A Simple 3 Layer Architecture