{"id":1033,"date":"2023-05-23T08:15:51","date_gmt":"2023-05-23T08:15:51","guid":{"rendered":"https:\/\/mustafauzun.co\/blog\/?p=1033"},"modified":"2023-05-23T08:15:51","modified_gmt":"2023-05-23T08:15:51","slug":"powershell-api-test-script","status":"publish","type":"post","link":"https:\/\/mustafauzun.co\/blog\/powershell-api-test-script\/","title":{"rendered":"Powershell, API Test Script"},"content":{"rendered":"\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\" data-file=\"API.ps1\"><code>Write-Host &quot;[API Test]&quot; &quot;`n&quot;\n\n$informationTexts = @{\n    Success = @{Text = &quot;Success&quot;; BackgroundColor = &quot;DarkGreen&quot;}\n    Error = @{Text = &quot;Error&quot;; BackgroundColor = &quot;DarkRed&quot;}\n    Warning = @{Text=&quot;Warning&quot;; BackgroundColor = &quot;DarkCyan&quot;}\n}\n\n$logFolder = &quot;logs&quot;\nNew-Item -ItemType Directory -Path $logFolder -Force | Out-Null\n\n$apis = @(\n    @{\n        &quot;name&quot;=&quot;Get Users&quot;\n        &quot;method&quot;=&quot;GET&quot;\n        &quot;url&quot;=&quot;api\/v2\/users?size=1&quot;\n        &quot;body&quot;=$null | ConvertTo-Json\n    },\n    @{\n        &quot;name&quot;=&quot;Post Users&quot;\n        &quot;method&quot;=&quot;POST&quot;\n        &quot;url&quot;=&quot;api\/v2\/users&quot;\n        &quot;body&quot;=@{\n            &quot;myParams1&quot;=&quot;hello&quot;\n        } | ConvertTo-Json\n    }\n)\n\n$header = @{\n&quot;Accept&quot;=&quot;application\/json&quot;\n&quot;Content-Type&quot;=&quot;application\/json&quot;\n}\n\n$apiDefaultURL = &quot;http:\/\/localhost:8081&quot;\n$apiURL = Read-Host -Prompt &quot;Input API website name (default: $apiDefaultURL)&quot;\n\nif(-not($apiURL))\n{\n    $apiURL = $apiDefaultURL\n}\n\nforeach ($api in $apis) {\n    Write-Host &quot;Request for $($api.name) API&quot; -NoNewline\n    Write-Host &quot; - &quot; -NoNewline\n\n    $response = Invoke-RestMethod -Uri $($apiURL + &#39;\/&#39; + $api.url) -Method $api.method -Body $api.body -Headers $header | ConvertTo-Json\n    \n    if($response.error) {\n        Write-Host $informationTexts.Error.Text -BackgroundColor $informationTexts.Error.BackgroundColor -NoNewline\n    } else {\n        Write-Host $informationTexts.Success.Text -BackgroundColor $informationTexts.Success.BackgroundColor -NoNewline\n    }\n\n    $logFile = &quot;$logFolder\\$($api.name).json&quot;\n    $response | Out-File -FilePath $logFile\n\n    Write-Host &quot; - &quot; -NoNewline\n    Write-Host &quot;(Response write to $logFile file)&quot;\n}\n\npause<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":1034,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-1033","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/1033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/comments?post=1033"}],"version-history":[{"count":1,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/1033\/revisions"}],"predecessor-version":[{"id":1035,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/1033\/revisions\/1035"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/media\/1034"}],"wp:attachment":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/media?parent=1033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/categories?post=1033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/tags?post=1033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}