Skip to content

Instantly share code, notes, and snippets.

@techthoughts2
Last active June 28, 2018 01:36
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 techthoughts2/4b7ed17c34fc4e12d67bc841b6e42783 to your computer and use it in GitHub Desktop.
Save techthoughts2/4b7ed17c34fc4e12d67bc841b6e42783 to your computer and use it in GitHub Desktop.
In this example (made possible by Mark Kraus's post: Multipart/form-data Support for Invoke-WebRequest and Invoke-RestMethod in PowerShell Core) we submit data via Inoke-WebRequest in a multipart format.
#Author: Jake Morrison - @jakemorrison - http://techthoughts.info/
#Contributor: Mark Kraus - @markekraus - from his post on PowerShell and Multipart/form-data - https://get-powershellblog.blogspot.com/2017/09/multipartform-data-support-for-invoke.html
#note: this was tested in PowerShell 6.0.2
try {
$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "chat_id"
$StringContent = [System.Net.Http.StringContent]::new("$ChatID")
$StringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)
$multipartFile = $PhotoPath
$FileStream = [System.IO.FileStream]::new($multipartFile, [System.IO.FileMode]::Open)
$fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$fileHeader.Name = "photo"
$fileHeader.FileName = $fileName
$fileContent = [System.Net.Http.StreamContent]::new($FileStream)
$fileContent.Headers.ContentDisposition = $fileHeader
$fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("image/png")
$multipartContent.Add($fileContent)
$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "caption"
$StringContent = [System.Net.Http.StringContent]::new($Caption)
$StringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)
$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "parse_mode"
$StringContent = [System.Net.Http.StringContent]::new($ParseMode.ToString())
$StringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)
<#
$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "disable_notification"
$StringContent = [System.Net.Http.Headers.]::new($true)
$StringContent.Headers.ContentDisposition = $stringHeader
$StringContent.Headers.ContentDisposition = $stringHeader
$fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("Boolean")
$multipartContent.Add($stringContent)#>
}#try_.NetMultiForm formation
catch {
Write-Warning "An error was encountered engaging .NET MultipartFormDataContent"
Write-Error $_
$results = $false
return $results
}#catch_.NetMultiForm formation
#------------------------------------------------------------------------
try {
$eval = Invoke-WebRequest -Uri $uri -Body $multipartContent -Method 'POST' -ErrorAction Stop
if (!($eval.StatusDescription -eq "OK")) {
Write-Warning -Message "Message did not send successfully"
$results = $false
}#if_StatusDescription
}#try_messageSend
catch {
Write-Warning "An error was encountered sending the Telegram photo message:"
Write-Error $_
$results = $false
}#catch_messageSend
return $results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment