Skip to content

Invoke-RestMethod does not return response headers  #9769

Closed

Description

@jianyunt

Background

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.

Actual behavior

headers are empty if server responses with 401.

Environment data

6.1

Activity

  1. added
    Issue-Question
    on May 30, 2019
  2. jianyunt commented on May 30, 2019

    @jianyunt
    ContributorAuthor
  3. vexx32 commented on May 30, 2019

    @vexx32
    Collaborator

    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?

  4. jianyunt commented on May 30, 2019

    @jianyunt
    ContributorAuthor

    thanks Rain Sallow (/u/ta11ow) (@vexx32) ! I fixed the typo in the repo.

  5. kilasuit commented on May 30, 2019

    @kilasuit
    Collaborator

    I can't repro this in 7-preview1 or 6.2.0 when running the below working code

    Invoke-RestMethod -Method Get -uri http://www.bing.com -ResponseHeadersVariable aa
    $aa 
  6. vexx32 commented on May 30, 2019

    @vexx32
    Collaborator

    Ryan Yates (@kilasuit) that's not a 401 response from the server though, right?

    Also, /cc Mark Kraus (@markekraus)

  7. jianyunt commented on May 31, 2019

    @jianyunt
    ContributorAuthor

    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.

  8. markekraus commented on May 31, 2019

    @markekraus
    Contributor

    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)

  9. felixfbecker commented on Jun 3, 2019

    @felixfbecker
    Contributor

    You can get the response headers from the error:

    try {
      Invoke-RestMethod '...'
    } catch [Microsoft.PowerShell.Commands.HttpResponseException] {
      $_.Exception.Response.Headers.WwwAuthenticate
    }

    Headers is a System.Net.Http.Headers.HttpResponseHeaders object.
    See also Microsoft.PowerShell.Commands.HttpResponseException.

  10. glawrence commented on Jun 3, 2019

    @glawrence

    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

  11. nmflavio commented on Oct 31, 2019

    @nmflavio

    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

  12. SteveL-MSFT commented on Nov 7, 2019

    @SteveL-MSFT
    Member

    This should also be addressed by #10466

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-QuestionResolution-FixedWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions