You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Invoke-RestMethod does not return response headers #9769
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues
I am responding my client request with 401 and sending back respond header with WWW-Authenticate: basic realm="apps" by following https://tools.ietf.org/html/rfc7235#section-4.1.
I can test it using postman. But I cannot use invoke-RestMethod because it does not return anything while 401.
Steps to reproduce
Invoke-RestMethod-Method Get -uri http://www.bing.comcomcomc -ResponseHeadersVariable aa
$aa# it's empty
Expected behavior
headers should be displayed even if server response with error code.
It's probably just a typo in the issue but just in case -- your code sample shows you're sending the headers to variable with name aa and then retrieving the variable $dd?
Ryan Yates (@kilasuit) yes, it works for happy cases. Try the uri with error.
Invoke-RestMethod -Method Get -uri http://www.bing.comdddddd -ResponseHeadersVariable aa
In my case, I need to challenge client with 401 and expect client send me auth token header, stuff.
a 401 response is treated a failed response and we do not populate the headers on fail responses. However, this would be worked around if we implement this #5555 (comment)
I have been struggling with this, note that httpbin.org is very handy for testing and that $_.Exception.Response.StatusCode.value__ seems to work on PowerShell 6.2.1 and 4.0. However I agree with others this should be easier especially with 2xx codes
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues
Activity
jianyunt commentedon May 30, 2019
Add Aditya Patwardhan (@adityapatwardhan)
vexx32 commentedon May 30, 2019
It's probably just a typo in the issue but just in case -- your code sample shows you're sending the headers to variable with name
aaand then retrieving the variable$dd?jianyunt commentedon May 30, 2019
thanks Rain Sallow (/u/ta11ow) (@vexx32) ! I fixed the typo in the repo.
kilasuit commentedon May 30, 2019
I can't repro this in 7-preview1 or 6.2.0 when running the below working code
vexx32 commentedon May 30, 2019
Ryan Yates (@kilasuit) that's not a
401response from the server though, right?Also, /cc Mark Kraus (@markekraus)
jianyunt commentedon May 31, 2019
Ryan Yates (@kilasuit) yes, it works for happy cases. Try the uri with error.
Invoke-RestMethod -Method Get -uri http://www.bing.comdddddd -ResponseHeadersVariable aa
In my case, I need to challenge client with 401 and expect client send me auth token header, stuff.
markekraus commentedon May 31, 2019
a 401 response is treated a failed response and we do not populate the headers on fail responses. However, this would be worked around if we implement this #5555 (comment)
felixfbecker commentedon Jun 3, 2019
You can get the response headers from the error:
Headersis aSystem.Net.Http.Headers.HttpResponseHeadersobject.See also
Microsoft.PowerShell.Commands.HttpResponseException.glawrence commentedon Jun 3, 2019
I have been struggling with this, note that httpbin.org is very handy for testing and that $_.Exception.Response.StatusCode.value__ seems to work on PowerShell 6.2.1 and 4.0. However I agree with others this should be easier especially with 2xx codes
nmflavio commentedon Oct 31, 2019
Try this one:
$resp = try { Invoke-RestMethod -Method Get -uri http://example.com/notfound } catch { $_.Exception.Response.Headers.ToString() }
PS C:\Users\Administrator> $resp
Content-Length: 94741
Cache-Control: private
Content-Type: text/html; charset=utf-8
Date: Thu, 31 Oct 2019 13:35:17 GMT
SteveL-MSFT commentedon Nov 7, 2019
This should also be addressed by #10466