Skip to content

Conversation

@jshigetomi
Copy link
Collaborator

@jshigetomi jshigetomi commented Jul 25, 2024

PR Summary

Fixes: #15409

When piping the stream is run to the end and the saved file size ends up being 0.

I added checks to see if a pipe was called then rewinded the stream.
If not piping, just save the stream in the file.
 
Wtih Invoke-RestMethod there isn't a consistent object returned when piped thru. The object can be a PSCustomObject, String, XML, or Json. So adding an OutFile like WebResponseObject is not feasible unless a new class is written for RestMethod like WebRequestObject.

PR Context

This fixes the issue where Invoke-RestMethod -PassThru -OutFile ends up not writing to file.
This issue is related to #21082.

PR Checklist

// Saves the response stream to a file. If piped then the response stream needs to be rewinded.
if (responseStream != null)
{
responseStream.Position = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to use the same code pattern we used for Invoke-WebRequest

responseStream = ro.RawContentStream;
responseStream.Seek(0, SeekOrigin.Begin);
}
if (ShouldSaveToOutFile)
{
WriteVerbose($"File Name: {Path.GetFileName(outFilePath)}");
// ContentLength is always the partial length, while ContentRange is the full length
// Without Request.Range set, ContentRange is null and partial length (ContentLength) equals to full length
StreamHelper.SaveStreamToFile(responseStream, outFilePath, this, response.Content.Headers.ContentRange?.Length.GetValueOrDefault() ?? response.Content.Headers.ContentLength.GetValueOrDefault(), perReadTimeout, _cancelToken.Token);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion! It took me one more look to realize how similar it can be. Changes made.

@jshigetomi jshigetomi marked this pull request as ready for review July 25, 2024 15:37
@microsoft-github-policy-service microsoft-github-policy-service bot added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels Aug 1, 2024
@daxian-dbw daxian-dbw merged commit 13e9aac into PowerShell:master Aug 5, 2024
@daxian-dbw daxian-dbw added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Aug 5, 2024
chrisdent-de pushed a commit to chrisdent-de/PowerShell that referenced this pull request Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invoke-RestMethod parameter combination of OutFile/PassThru does not write to file as intended

3 participants