-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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
Uploading file via Invoke-RestMethod corrupts file #21386
Comments
A couple of questions, why use Invoke-RestMethod rather than Invoke-WebRequest ? Why are you converting the byte data to string when you are saying in the content-type it is application/octet-stream? That looks like part of the corruption problem. As an alternative using forms and PowerShell, this is an example of something I use locally
|
The problem stems from There should be two ways of doing this
The $file = Get-Item /tmp/test.txt
$fileStream = $file.OpenRead()
$content = [System.Net.Http.MultipartFormDataContent]::new()
$content.Add(
[System.Net.Http.StreamContent]::new($fileStream),
"Filedata",
$file.Name)
# Content-Type is set automatically from the FormData to
# multipart/form-data, Boundary: "..."
Invoke-RestMethod -Uri $uploadUri -Method Post -Body $content The multipart content is definitely verbose but it allows you to use the .NET objects to build the form data needed, in this case it's including the When looking at the raw value sent with the above I found that .NET adds a second $content = [System.Net.Http.MultipartFormDataContent]::new()
$fileContent = [System.Net.Http.StreamContent]::new($fileStream)
$fileContent.Headers.ContentDisposition = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
# Your example had quotes in your literal form-data example so I kept them here
$fileContent.Headers.ContentDisposition.Name = '"Filedata"'
$fileContent.Headers.ContentDisposition.FileName = '"{0}"' -f $file.Name
$fileContent.Headers.ContentType = 'application/octet-stream'
$content.Add($fileContent) |
Thank you very much @jborean93 ! Thanks & greetings, |
📣 Hey @Binomimus, how did we do? We would love to hear your feedback with the link below! 🗣️ 🔗 https://aka.ms/PSRepoFeedback |
Thanks @rhubarb-geek-nz |
Prerequisites
Steps to reproduce
When uploading a file to Citrix Sharefile via API the file somehow gets corrupted. I am uploading a 7-zip archive and the uploaded file is about 50% larger than the original and cannot be opened afterwards (e.g. source file is 180.930 bytes, uploaded file is 271.815 bytes). That issue does not appear when using PowerShell 7.3.5. It first appeared after upgrading to 7.4.1
Example code
The URI for uploading is generated like described in the ShareFile API documentation. See "Upload file" here: https://api.sharefile.com/docs/resource?name=Items
Expected behavior
Uploaded file is same as original
Actual behavior
Uploaded file is larger and corrupted
Error details
There is no error. From PowerShell perspective everything seems to be fine...
Environment data
Visuals
No response
The text was updated successfully, but these errors were encountered: