'Powershell add custom parameters to MultipartForm HTTP request

I would like to upload file(s) to PHP server using powershell, Am not in powershell bellow is a code which worked but i don't know how to add extra parameters to be submitted together with files.

I tried

$content.Add("description", "Description of files")

That gave an error - Cannot find an overload for "Add" and the argument count: "2"

Code

$ErrorActionPreference = 'Stop'

$fieldName = 'file'
$filePath = 'C:\file.txt'
$url = 'http://127.0.0.1/powershell/upload.php'

$fieldName2 = 'file2'
$filePath2 = 'C:\file2.txt'


Try {
    Add-Type -AssemblyName 'System.Net.Http'

    $client = New-Object System.Net.Http.HttpClient
    $content = New-Object System.Net.Http.MultipartFormDataContent

    $fileStream = [System.IO.File]::OpenRead($filePath)
    $fileName = [System.IO.Path]::GetFileName($filePath)
    $fileStream2 = [System.IO.File]::OpenRead($filePath2)
    $fileName2 = [System.IO.Path]::GetFileName($filePath2)

    $fileContent = New-Object System.Net.Http.StreamContent($fileStream)
    $fileContent2 = New-Object System.Net.Http.StreamContent($fileStream2)

    $content.Add($fileContent, $fieldName, $fileName)
    $content.Add($fileContent2, $fieldName2, $fileName2)

    $content.Add("description", "Description of files")

    $result = $client.PostAsync($url, $content).Result
    $result.EnsureSuccessStatusCode()
}
Catch {
    Write-Error $_
    exit 1
}
Finally {
    if ($client -ne $null) { $client.Dispose() }
    if ($content -ne $null) { $content.Dispose() }
    if ($fileStream -ne $null) { $fileStream.Dispose() }
    if ($fileContent -ne $null) { $fileContent.Dispose() }
    if ($fileStream2 -ne $null) { $fileStream2.Dispose() }
    if ($fileContent2 -ne $null) { $fileContent2.Dispose() }
}

How do i properly add extra parameters to the script?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source