powershellgetziphttpwebresponse

How Do I GET a StreamResponseBody and save it as a ZIP file using Powershell


I am trying to extract a streaming response from a standard http GET call using PowerShell.

I tried to do something like using this Invoke-RestMethod method, such as:

$response = Invoke-RestMethod -Uri $uri -Method GET -Headers $headers -Body 

and then tried doing various things like accessing the streaming response in this way:

$Stream.Write($response.Content)

or

$Stream.Write($response.RawContentStream)

but nothing works.


Solution

  • In the end, I found out the following command does both the call and the response along with allowing you to save the result to a file directly. This surprised me because the response is a multipart record that includes headers and other data, not just the chunked file in the stream. But the folks at MS apparently knew that 99% of the time with a chunked reply, the user only wants to save the streamed chunks of data into the file, without the other response header data.

      $Response = Invoke-WebRequest @Params -Headers $headers -outfile $TempZipFileName
    

    The key here is using the Invoke-WebRequest call with the -outfile option. This saved me a lot of headache and was super simple to use.