Testing REST API with PowerShell

PowerShell

 Here is a simple way we can test a REST API with PowerShell:

Invoke-RestMethod -Uri https://path/to/api -Method Post -ContentType "application/json" -Body '{ "key": "val" }'

Then, in order to convert the result into a readable JSON add this:

| ConvertTo-Json -Depth 10

If the response is XML, e.g when sending a SOAP request, the the result is an XML document object. Keep the result in a variable and print the contents with OuterXml property, like so:


$result = Invoke-RestMethod -Uri https://path/to/api
$result.OuterXml

Here is another example of using a GET request with the basic authentication authorization header:


$headers = @{ Authorization="Basic .........." } 
Invoke-RestMethod -Method Get -Uri "https://google.com/api/GetData" -Headers $headers

Use this URL: https://www.debugbear.com/basic-auth-header-generator to generate the authorization header based on your username and password.

Post a Comment

Previous Post Next Post