Powershell v3 Invoke-RestMethod [英] Powershell v3 Invoke-RestMethod

查看:344
本文介绍了Powershell v3 Invoke-RestMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用新的invoke-restmethod cmdlet来POST JSON文件,并且已成功完成。但是,我没有收到来自Web服务器的响应,就像我在使用CURL时。对于我想要完成的工作,我需要把信息从reposne到POST,并用于另一个POST命令。

I am attempting to make use of the new invoke-restmethod cmdlet to POST a JSON file and have succesfully done so. However, I do not receive a response from the webserver like I did when using CURL. For what I'm trying to accomplish I need to take info from the reposne to the POST and use this for another POST command.

有人可以解释一下来自服务器的预期响应?下面是两个命令1st在CURL,第二使用Invoke-RestMethod。 curl命令将执行正确的POST并返回响应。 Powershell命令将执行正确的POST,但不会返回响应。

Can someone please explain how I can get the expected response from the server? Below are the two commands 1st in CURL, 2nd using Invoke-RestMethod. The curl command will perform the correct POST and return a response. The Powershell command will perform the correct POST but will not return a response.

感谢

我相信我想从ps输出获得的主要是响应头ie。下面的输出从curl命令

edit: The main thing I believe I am trying to get from ps output is the "response headers" ie. the output below from a curl command

 < 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 Command

 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代码 p>

Powershell Code

 $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


推荐答案

在一些钓鱼后,我发现了cmdlet Invoke-WebRequest 。这个cmdlet基本上与 Invoke-RestMethod 相同,除了它返回标头以及响应之外。

After some fishing around I discovered the cmdlet Invoke-WebRequest. This cmdlet is basically identical to Invoke-RestMethod other than the fact that it returns the headers as well as response.

这篇关于Powershell v3 Invoke-RestMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆