首页 > 解决方案 > cmdlet 无法运行,因为 -ContentType 参数不是有效的 Content-Type 标头

问题描述

嗨,我在我的 devops 管道中运行了以下 Powershell cmdlet,该管道正在将工件上传到 sharePoint 库中。到现在为止一切正常,但我在其中一个项目中遇到了错误。不知道我该如何解决这个问题。如果有人知道这里的任何事情。

我的 Powershell 代码>

    $fileSize = (Get-Item -LiteralPath $file).length
    write-host "*******"
    write-host $file
    write-host $fileSize
    write-host "*******"
    $uploadURLObject = GET-UPLOADLINK -clientId $clientId -clientSecret $clientSecret -nameofFile $nameWithoutExtension
    $tokenObject = GET-TOKEN -clientId $clientId -clientSecret $clientSecret
    $uploadHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $uploadHeaders.Add("Content-Type", "application/zip")
    $uploadHeaders.Add("Content-Range", "bytes " + 0 +"-" + ($fileSize-1) + "/" + $fileSize)
    $uploadHeaders.Add("Content-Length", $fileSize)
    $uploadHeaders.Add("Authorization", "Bearer "+ $tokenObject.access_token)    
   # $uploadBody = [System.IO.File]::ReadAllBytes($file)    
    $uploadBody = Get-Content -LiteralPath $file
    write-host $uploadBody
    $response = Invoke-RestMethod $uploadURLObject.uploadUrl -Method 'PUT' -Headers $uploadHeaders -Body $uploadBody
   # $response = Invoke-RestMethod $uploadURLObject.uploadUrl -Method 'PUT' -Headers $uploadHeaders -Infile $file -SkipHeaderValidation 
    $response | ConvertTo-Json  

错误信息

Invoke-RestMethod :无法运行 cmdlet,因为 -ContentType 参数不是有效的 Content-Type 标头。为 -ContentType 指定一个有效的 Content-Type,然后重试。要禁止标头验证,请提供 -SkipHeaderValidation 参数。

更新:我检查了我的 Headers 键和值,似乎没有什么异常。

Key: Content-Type    Value: application/zip
Key: Content-Range    Value: bytes 0-66704/66705
Key: Content-Length    Value: 66705
Key: Authorization    Value: Bearer rzoQ6Ls7YoaQGls8MBlPVScBPA...dfd

标签: powershell

解决方案


推荐阅读