+2 votes
Do this:

$file = Get-Content -Path "C:\1.txt"
$uri = "https://api.telegram.org/botXXXX/sendDocument?chat_id=XXXXX&document"
Invoke-RestMethod -Method Post -Uri $uri -ContentType 'multipart/form-data' -Body [byte]$file

При выполнении ошибка:
Invoke-RestMethod : The remote server returned an error: (400) Bad Request.

прошу помощи, заранее благодарен

2 Answers

0 votes
Best answer
from Windows Powershell, there is no direct support for 'multipart/form-data', only the Core

In particular, for multipartне enough guidance bondaryв ContentType
You can try to experiment with the manual Assembly such as here @jklemmack
or with System.Net.Http.MultipartFormDataContent
0 votes
in the 7th (and, it seems, in the 6th) powersale works like this:

$token = "ХХХХХХХХХХХ"
$chat_id = "ХХХХХХХХХ"
$uri = "https://api.telegram.org/bot$Token/sendDocument"
$fileObject= get-item C:\test\1.pdf
$Form = @{
chat_id = $Chat_ID
document = $fileObject

$invokeRestMethodSplat = @{
Uri = $Uri
ErrorAction = 'Stop'
Form = $Form
Method = 'Post'

Invoke-RestMethod @invokeRestMethodSplat

в 5ом нет параметра форм в Invoke-RestMethod и там нужны пляски с бубном.