Is there a way to fetch Response header for InvokeRest Method of Powershell Version 5

6.2k Views Asked by At

How can I fetch Response header for InvokeRest Method of Powershell Version 5, For Powershell version 6 -ResponseHeadersVariable is available , Is there any way to do it in powershell version 5. I am unable to find any way to do it.

2

There are 2 best solutions below

1
On BEST ANSWER

I don't think there is a way to get response headers of Invoke-RestMethod in v5 but you can easily replace that with something like

$headers = Invoke-WebRequest 'example.com' | select headers

Adding some context of your scenario can help to suggest more suitable approach.

0
On

I am using powershell core and Invoke-RestMethod now has a parameter for getting response headers.

♥ pwsh::github$ $null = Invoke-RestMethod -Uri https://google.com -ResponseHeadersVariable rh
♥ pwsh::github$ $rh
Key                                 Value
---                                 -----
Date                                {Tue, 15 Aug 2023 21:43:50 GMT}
Cache-Control                       {max-age=0, private}
Content-Security-Policy-Report-Only {object-src 'none';base-uri 'self';script
...........