在松弛状态下发布图像时获取“ invalid_form_data”

时间:2019-10-25 09:48:05

标签: powershell slack slack-api

我有一张要发布到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 :(得分:0)

我已经解决了这个问题,我不想使用可用的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