Получение «invalid_form_data» при публикации изображения в slack - PullRequest
0 голосов
/ 25 октября 2019

У меня есть изображение, которое я пытаюсь опубликовать в канале Slack, у меня есть следующий скрипт в powershell

$header = @{
 "Accept"="*/*"
 "token"="f6b8a3cf-d78e-471d"
 "Content-Type"="multipart/form-data"
}
    function uploadScreenshots {
        $path = "D:\Demo\PNG\Test.png"
        $url = "https://slack.com/api/files.upload"
        $Form = @{
             channels = "poc_test"
             file = $path
            }
        $response = Invoke-WebRequest -uri $url -Header $header -Method POST  -Body $Form 
        write-host $response
        }
    uploadScreenshots

Когда я использую почтальон, он может успешно загрузить изображение, но вышескрипт дает мне "invalid_form_data"

{"ok":false,"error":"invalid_form_data"}

Почтальон Подробности:

curl --request POST \
  --url https://slack.com/api/files.upload \
  --header 'Accept: */*' \
  --header 'Accept-Encoding: gzip, deflate' \
  --header 'Cache-Control: no-cache' \
  --header 'Connection: keep-alive' \
  --header 'Content-Length: 122013' \
  --header 'Content-Type: multipart/form-data; boundary=--------------------------125774860738041511758992' \
  --header 'Host: slack.com' \
  --header 'Postman-Token: 8add1159-ceb77250d51f' \
  --header 'User-Agent: PostmanRuntime/7.17.1' \
  --header 'cache-control: no-cache' \
  --header 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  --form file=@/D:/Demo/PNG/Test.png \
  --form 'channels=poc_test' \
  --form token=f6b8a3cf-d78e-471d

1 Ответ

0 голосов
/ 28 октября 2019

Я исправил проблему, я не хочу использовать модуль slack. Я просто хочу отправить изображение через запрос API.

function uploadScreenshots {
    $path = 'D:\Demo\PNG\Test.png'
    $Channel ='poc_test'
    $uri = "https://slack.com/api/files.upload" 
    $token = "<sec_token>"
    $LF = "`r`n"
    $readFile = [System.IO.File]::ReadAllBytes($Path)
    $enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")
    $fileEnc = $enc.GetString($readFile)
    $fileName = 'abc'
    $boundary = [System.Guid]::NewGuid().ToString()
    $bodyLines =
                "--$boundary$LF" +
                "Content-Disposition: form-data; name=`"file`"; filename=`"$fileName`"$LF" +
                "Content-Type: 'multipart/form-data'$LF$LF" +
                "$fileEnc$LF" +
                "--$boundary$LF" +
                "Content-Disposition: form-data; name=`"token`"$LF" +
                "Content-Type: 'multipart/form-data'$LF$LF" +
                "$token$LF"
    $bodyLines +=
                ("--$boundary$LF" +
                "Content-Disposition: form-data; name=`"channels`"$LF" +
                "Content-Type: multipart/form-data$LF$LF" +
                ($Channel -join ", ") + $LF)
                $bodyLines += "--$boundary--$LF"
    try {
        $Params = @{
            Uri = $uri
            Method = 'Post'
            ContentType = "multipart/form-data; boundary=`"$boundary`""
            Body = $bodyLines
            Verbose = $true
        }
        $response = Invoke-RestMethod @Params
        write-host $response
    }
    catch [System.Net.WebException] {
        Write-Error( "Rest call failed for $uri`: $_" )
                    throw $_
    }

}
uploadScreenshots
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...