$Headers = @{
"Auth-Token" = "jD9mGkRSGDFF575474OeBoi6bp"
"User-Id" = "8234965283592"
}
$Uri = 'https://site.com/api/setAvatar';
$FileContent = [IO.File]::ReadAllBytes('C:Temp\test.png');
$Fields = @{'username'='a.sidorov';'image'=$($FileContent)};
Invoke-RestMethod -Uri $Uri -ContentType 'multipart/form-data' -Method Post -Headers $Headers -Body $Fields;
$ErrorActionPreference = 'Stop'
$fieldName = 'file'
$filePath = 'C:\Temp\test.pdf'
$url = 'http://posttestserver.com/post.php'
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)
$fileContent = New-Object System.Net.Http.StreamContent($fileStream)
$content.Add($fileContent, $fieldName, $fileName)
$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() }
}
Add-Type -AssemblyName System.Web
Add-Type -AssemblyName System.Net.Http
$uri = "https://api.telegram.org/bot$code/sendDocument"
$form = New-Object System.Net.Http.MultipartFormDataContent
$chatId = New-Object System.Net.Http.StringContent $Chat_ID
$FileName = "D:\mydoc.txt"
$form.Add($chatId, 'chat_id')
$fileContent = Get-Content $FileName -Encoding Byte
$byteContent = New-Object System.Net.Http.ByteArrayContent ($fileContent, 0, $fileContent.Length)
$byteContent.Headers.Add('Content-Type',[System.Web.MimeMapping]::GetMimeMapping($FileName))
$form.Add($byteContent, 'document', (Split-Path $filename -Leaf))
$ms = New-Object System.IO.MemoryStream
$ca = $form.CopyToAsync($ms)
$ca.Wait()
Invoke-WebRequest -Method Post -Body $ms.ToArray() -Uri $uri -ContentType $form.Headers.ContentType.ToString()