Parameterized PowerShell API test script
Need to test an XML API that uses basic authorization. Created this script with the assumption you have real cURL installed and to allow for automated parameterization of the script output.
param ($externalId, $inputBaseUrl, $inputUser, $inputApiKey)
function formatXml([string]$xmlcontent) {
$xmldoc = new-object -typename system.xml.xmldocument
$xmldoc.loadxml($xmlcontent)
$sw = new-object system.io.stringwriter
$writer = new-object system.xml.xmltextwriter($sw)
$writer.formatting = [system.xml.formatting]::indented
$xmldoc.writecontentto($writer)
return $sw.tostring()
}
$id = "1234"
if ($externalId) {
$id = $externalId
}
$baseUrl = "https://somebaseurl.invalid";
if ($inputBaseUrl) {
$baseUrl = $inputBaseUrl
}
$userId = "user1";
if ($inputUser) {
$userId = $inputUser
}
$apiKey = "apiHeader"
if ($inputApiKey) {
$apiKey = $inputApiKey
}
$basicAuth = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$($userId):$($apiKey)"))
$testUrl = "${baseUrl}/${id}/segment1/?user=${userId}&apiKey=${$apiKey = "apiHeader"
}"
$authHeader = "Authorization: Basic ${basicAuth}"
$apiResponse = curl --location $testUrl --header $authHeader
formatXml($apiResponse)
I then combined it with Set-Clipboard
for individual single script verification like so
.\run_api_tester_script.ps1 | Set-Clipboard