I wrote a small PowerShell script to send a request to a server and get a simple XML result back.
PowerShell Script
$xml = "<?xml version='1.0' encoding='utf-8'?><Search><ID>278A87E1-1BC2-4E19-82E9-8BBE31D67D20</ID></Search>"
$response = Invoke-RestMethod -Method Post -Uri "http://localhost/search" -ContentType "application/xml" -Body $xml
That's it, really simple and there's no reason that I can see for it to be failing. I have also tried the script with Invoke-WebRequest
and both fail. The error returned is Invoke-RestMethod : Value cannot be null. Parameter name: name
. The strange thing is that when I monitor this with Wireshark
, I see the connection, I see the POST
and I see the result from the server and all looks perfectly good but the cmdlet is saying it failed (and yes, the return code is 200).
If I run the Invoke-WebRequest
/Invoke-RestMethod
with the -OutFile
parameter, it runs fine with no errors and saves the result to the specified file; -OutVariable
fails just in case you're wondering.
The result is an xml file, the headers specify that it is xml and the xml is properly formatted.
Result when successful
<?xml version="1.0" encoding="UTF-8" ?>
<Result version="1.0" xmlns="urn:xmlns-org">
<searchID>{278a87e1-1bc2-4e19-82e9-8bbe31d67d20}</searchID>
<responseStatus>true</responseStatus>
<responseStatusStrg>MORE</responseStatusStrg>
<numOfMatches>40</numOfMatches>
</Result>
Does anyone know why the Invoke-XXX
cmdlets are returning an error and what I can do to fix it? Again, it works perfectly fine when I use the -OutFile
parameter and even when it fails I can see a proper conversation between the script and the server in Wireshark
.
Also, if I use -Verbose
it tells me the following:
VERBOSE: POST http://localhost/search with -1-byte payload
VERBOSE: received X-byte response of content type application/xml; charset="UTF-8"
Where X-byte
is the actual size of the response but it obviously differs with each response depending on the data sent to the server. I just find it odd that the cmdlet fails but says it received a response with data and that it sent a -1-byte
payload.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…