techthoughts2
6/28/2018 - 1:33 AM

PowerShell Multipart/form-data

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