Command to retrieve response header information when using Invoke-Restmethod RRS feed

  • Question

  • I use following command to download a file.

    Invoke-RestMethod -Method Post -Body $obj -ContentType 'application/x-www-form-urlencoded' -Uri $downloadurl -Verbose -OutFile "$downloaddir$i.tar"
    The above command's out is below:
    OUTPUT: File is returned as the response object.
    I need to get login key and token key for further Invoke-Restmethod communication with the system e.g. in order to download any additional file etc.
    I am told the logon key and token key have to be extracted from the response headers as the file is returned instead of a JSON object.
    I need help how to extract above information, what command to be used?

    Monday, August 7, 2017 2:35 AM

All replies

  • No command. Just reference the "ResponseHeaders" in the returned object.


    Monday, August 7, 2017 2:46 AM
  • No command. Just reference the "ResponseHeaders" in the returned object.


    Will you  kindly provide an example? The previous command downloads the file in octet stream under OutFile location.


    • Edited by shalini217 Tuesday, August 8, 2017 2:40 AM
    Tuesday, August 8, 2017 2:36 AM
  • $response = Invoke-RestMethod ....

    Don't use "OutFile"

    Assuming that there are headers.  Without the ability to call you method this issue cannot be easily demonstrated.


    Tuesday, August 8, 2017 2:43 AM
  • I still need help for responseHeaders

    I tried below, but it doesn't do the desired work:

    $response=Invoke-RestMethod -Method Post -Body $obj -ContentType 'application/x-www-form-urlencoded' -Uri $downloadurl -Verbose

    Write-Output $response.responseHeaders

    I don't see anything displayed on the screen after octet stream is returned for $response. Nothing is returned for $response.responseHeaders


    VERBOSE: received 56401920-byte response of content type


    Also how can I save the response file in a directory?

    Sunday, August 13, 2017 4:16 AM