-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fixes Invoke-RestMethod -PassThru -Outfile writing to file. #24086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| // Saves the response stream to a file. If piped then the response stream needs to be rewinded. | ||
| if (responseStream != null) | ||
| { | ||
| responseStream.Position = 0; |
There was a problem hiding this comment.
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
Lines 61 to 72 in 0f13f45
| 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); | |
| } |
There was a problem hiding this comment.
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.
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
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.- [ ] Issue filed:
(which runs in a different PS Host).