Created
September 24, 2023 01:44
Post a file to a URL with Powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# upload file with form-data to a URL using powershell | |
# this works with binary files, no conversion happens to the file | |
# | |
# this can be used to deploy files on Appveryor | |
$File='result.zip'; | |
$FilePath = Get-Item -Path $File; | |
$URL = "https://example.com/upload.php"; | |
$fileBytes = [System.IO.File]::ReadAllBytes($FilePath); | |
$fileEnc = [System.Text.Encoding]::GetEncoding('iso-8859-1').GetString($fileBytes); | |
$boundary = [System.Guid]::NewGuid().ToString(); | |
$EOL = "`r`n"; | |
$bodyLines = ( | |
"--$boundary", | |
"Content-Disposition: form-data; name=`"file`"; filename=`"$File`"", | |
"Content-Type: application/octet-stream", | |
"", | |
$fileEnc, | |
"--$boundary", | |
"Content-Disposition: form-data; name=`"filename`"", | |
"", | |
$File, | |
"--$boundary", | |
"Content-Disposition: form-data; name=`"apikey`"", | |
"", | |
"abcd", | |
"--$boundary--", | |
"" | |
) -join $EOL | |
Invoke-RestMethod -Uri $URL -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment