As DevOps engineers, you get requirements to do scripts to perform several actions on application or get status of the application backend using API calls. This post explains small issue that we experience while writing PowerShell scripts to trigger APIs.
Issue: Invoke-RestMethod cannot be used to get the return HTTP status from the API. API call failed with mentioning -StatusCodeVariable is not valid. You can learn more about Invoke-RestMethod from here.
Reason : -StatusCodeVariable parameter cannot be used to get API call status by Invoke-RestMethod with Powershell version 5.1. It is supported in PowerShell version 7 but not in 5.1
Following image has the code sample for get HTTP status in PowerShell 7 with Invoke-RestMethod
Fix : You can update the powershell version to 7 or you can use Invoke-WebRequest in PowerShell 5.1, instead of Invoke-RestMethod method. You can learn more about Invoke-WebRequest from here.
Following image has the code sample for get HTTP status in PowerShell 5.1 with Invoke-WebRequest
This blog explained an issue faced while working with API calls and simple fix by introducing Invoke-WebRequest instead of Invoke-RestMethod
No comments:
Post a Comment