Skip to content

Instantly share code, notes, and snippets.

@alexlehm
Created September 24, 2023 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexlehm/bae63d78bd59c46f4170d27766d2db85 to your computer and use it in GitHub Desktop.
Save alexlehm/bae63d78bd59c46f4170d27766d2db85 to your computer and use it in GitHub Desktop.
Post a file to a URL with Powershell
# 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