需要帮助将Curl语句翻译为Powershell

时间:2017-01-27 22:10:14

标签: powershell curl

我试图重写以下卷曲代码:(注意我已经使用OBSCURED来隐藏秘密)

curl -H "Content-Type: application/json" \ 
     -H "Authorization: Bearer OBSCURED" \
     -X POST \
     -d "{\"color\": \"pink\", \"message\": \"OBSCURED\" }" \
     https://OBSCURED
我可以在Powershell中使用的东西,但我遇到了很多悲伤......这是我到目前为止的尝试:

$body = @{
color = "pink"
message = "OBSCURED"
}

Invoke-WebRequest -ContentType application/json -Headers @{"Authorization" = "Bearer OBSCURED"} -Method Post -Body "$body" -Uri https://OBSCURED

但是我遇到了这个PowerShell错误:

Invoke-WebRequest : The remote server returned an error: (405) Method Not Allowed.At E:\BuildAgent\temp\buildTmp\powershell2561816976397359486.ps1:16 char:1
Invoke-WebRequest -Headers @{"Authorization" = "Bearer OBSCURED ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

1 个答案:

答案 0 :(得分:0)

我现在无法测试,但我相信您需要提交JSON作为正文,因此您可以尝试使用convertto-JSON将您的正文转换为JSON,然后尝试发布:

$bodyJSON = body |convertto-JSON

然后致电:

Invoke-WebRequest -ContentType application/json -Headers @{"Authorization" = "Bearer OBSCURED"} -Method Post -Body $bodyJSON -Uri https://OBSCURED