8
votes

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.

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.

Thanks

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 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
1
The docs say this cmdlet is supposed to send the results automatically down the pipeline unless you also specify -OutFile path-to-file. It also claims the cmdlet outputs XmlDocument, HtmlWebResponseObject and string. It clearly seems like this should work. I would try using -OutFile c:\response.txt as a potential work-around. If that doesn't work, I'd say it is time to file a bug. BTW, which build of V3 are you using?Keith Hill
I think I may have stated my question incorrectly. I am indeed getting output from the powershell command, but I am not getting everything that the curl command outputs. Most notably the request header. I will restate my question above.floyd
What output do you get? Try running the output through | Get-Member to see what type of object you're getting. Keep in mind that by default, PowerShell doesn't always display all the information that is available on an object.Keith Hill

1 Answers

18
votes

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.