powershell - Invoke-RestMethod:发送到 header 时,无法根据请求找到身份验证数据

标签 powershell post azure-devops http-headers

我在azure devops版本中添加了powershell脚本,并尝试使用带有授权 token 和xml文件的Invoke-WebRequest发送POST请求。

我究竟做错了什么?

我从另一个POST请求中获得了授权 token ,并尝试使用此 token 创建 header ,并尝试使用X射线端点发送xml文件(NUnit XML结果-POST / api / v1 / import / execution / nunit)
https://confluence.xpand-it.com/display/XRAYCLOUD/Import+Execution+Results+-+REST#ImportExecutionResults-REST-NUnitXMLresults

授权 token ,例如 header 参数
内容类型(如Powershell参数)

$bodyForAuth=@{"client_id"="...";
"client_secret"="...";
}
$jsonAuth=Invoke-WebRequest -Uri $urlForAuth -Method POST -Body ($bodyForAuth|ConvertTo-Json) -ContentType "application/json"
$authToken=ConvertFrom-Json -InputObject $jsonAuth

$headers=@{ Authorization = "Bearer $authToken" }

Invoke-WebRequest -Headers $headers -Uri $urlForXrayApi -Method POST -ContentType "text/xml" -InFile $TestResultsXmlPath

我有错误:“Invoke-RestMethod:找不到请求中的身份验证数据”

我也尝试这样发送:
$headers=@{ Authorization = "Bearer $authToken";"Content-Type"="text/xml" }

Invoke-WebRequest -Headers $headers -Uri $urlForXrayApi -Method POST -InFile $TestResultsXmlPath

而且也有同样的错误:"Invoke-RestMethod : Could not find authentication data on request"
但是,如果我发送这样的POST请求(没有Content-Type):
$bodyForAuth=@{"client_id"="...";
"client_secret"="...";
}
$jsonAuth=Invoke-WebRequest -Uri $urlForAuth -Method POST -Body ($bodyForAuth|ConvertTo-Json) -ContentType "application/json"
$authToken=ConvertFrom-Json -InputObject $jsonAuth

$headers=@{ Authorization = "Bearer $authToken" }

Invoke-WebRequest -Headers $headers -Uri $urlForXrayApi -Method POST -InFile $TestResultsXmlPath

我有伺服器错误:"Invoke-RestMethod : {"error":"Missing data in the nunit results import request"}"
Power Shell v.5.1

为什么当我发送到 header 服务器时无法对授权数据进行罚款?

最佳答案

我在请求中错过了“(”和“)”
Invoke-WebRequest -Headers $headers -Uri $urlForXrayApi -Method POST -InFile $TestResultsXmlPath
应该
Invoke-WebRequest -Headers $headers -Uri $urlForXrayApi -Method POST -InFile $(TestResultsXmlPath)
$TestResultsXmlPath更改为$(TestResultsXmlPath)

关于powershell - Invoke-RestMethod:发送到 header 时,无法根据请求找到身份验证数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56610057/

相关文章:

ios - 快速发送 POST 请求但收到 405 响应(不允许的方法)

docker - 是否可以在 Azure DevOps Pipeline 中运行 GitHub Workflow Actions?

azure-devops - 来自变量模板的 Azure Pipelines 参数值

java - 如何使用PowerShell脚本在Windows上启动本地neo4j服务器?

xml - 无法阻止 PowerShell 更改返回对象的类型

powershell - 在 Powershell 中调用批处理脚本时如何传递等号?

javascript - 在 Express 中接收 Jquery POST 数据

php外部站点登录然后转到另一个页面

Powershell - 在 input-csv 命令期间添加 header

asp.net - 在 Azure Web App 上预编译 ASP.NET MVC View