I’m trying to invoke api calls using Powershell script. could someone help me out on how to get the response headers? I want to get the x-total-count value.
@chandramohan27 Everything is good except you need to escape the double quotes and alter the token in the header.
There are a few ways to escape the double quotes but one way is to put 2 (“”) like this around sourceName. Also notice the token is being set as the Authorization with a Bearer type. This example also utilizes splatting, where the parameters for the Invoke-RestMethod are set and then passed to that command all at once.
In order to get the X-Total-count, you can use PowerShell 7 (technically just 6) as the ResponseHeaderVariable was added. Otherwise you can also use Invoke-WebRequest.
As @colin_mckibben suggested, the best and easiest way would be the PowerShell SDK.
While, if you like to stick to your own PowerShell, you can convert the response to JSON using
$response = Invoke-WebRequest -Method Get -Uri $uri -Headers $headers
# Retrieve the JSON response body
$jsonResponse = $response.Content
# You can parse the JSON response body if needed
$parsedResponse = ConvertFrom-Json $jsonResponse
# Convert headers to JSON
$headersJson = $response.Headers | ConvertTo-Json