我正在尝试使用新的invoke-restmethod cmdlet来发布JSON文件,并已成功完成此操作。但是,我没有收到Web服务器的响应,就像使用CURL时所做的那样。对于我试图完成的工作,我需要从reposne获取信息到POST并将其用于另一个POST命令。Powershell v3 Invoke-RestMethod
有人可以请解释我如何能从服务器获得预期的响应?下面是CURL中第一个使用Invoke-RestMethod的第一个命令。 curl命令将执行正确的POST并返回响应。 Powershell命令将执行正确的POST,但不会返回响应。
感谢
编辑:我相信,我想从ps的输出,以获得主要的是“响应头”,即。从curl命令
< HTTP/1.1 201 Created
< Date: Thu, 26 Jul 2012 01:20:06 GMT
< Server: Apache
< X-EM7-Implemented-methods: GET,PUT,POST
< X-Powered-By: ScienceLogic,LLC - EM7 API/Integration Server
< Location: /ticket/321750
< X-EM7-status-message: ticket /ticket/321750 added.
< X-EM7-status-code: CREATED
< Content-Length: 830
< Content-Type: application/json
<
curl命令
curl -f -v -s -k --no-sessionid -H X-em7-beautify-response:1 -H content- type:application/json https://URLHERE --data-binary @jsonfile.json
PowerShell代码
$cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("certfile.crt")
$json = Get-Content jsonfile.json
$cred = Get-Credential -Message "Enter Credentials"
Invoke-RestMethod -Uri https://URLHERE -Credential $cred -Body $json -Certificate $cert -ContentType application/json -Method POST
文档说,除非您还指定'-OutFile path-to-file',否则此cmdlet应自动将结果向管道发送。它还声称该cmdlet输出XmlDocument,HtmlWebResponseObject和字符串。很明显,这似乎应该起作用。我会尝试使用'-OutFile c:\ response.txt'作为潜在的解决方法。如果这不起作用,我会说现在是时候提交一个错误。顺便说一句,你使用哪个版本的V3? – 2012-07-26 01:39:09
我想我可能错误地陈述了我的问题。我确实从powershell命令获得输出,但我没有得到curl命令输出的所有内容。最值得注意的是请求头。我会在上面重申我的问题。 – floyd 2012-07-26 01:49:30
你会得到什么输出?尝试通过'|运行输出Get-Member'来查看你得到的是什么类型的对象。请记住,默认情况下,PowerShell并不总是显示对象上可用的所有信息。 – 2012-07-26 02:42:39