Reputation: 21
I'm calling an SOAP API. PowerShell is giving an error and the error contains the response I am expecting! Whereas the contents of the variable I'm sending the response to is missing some data.
[xml]$response = Invoke-WebRequest -Uri $UserURI -Method POST -ContentType "test/xml;charset=utf-8" -Body $input
$SOAPResponse = [xml]$response.Content
PowerShell is returning:
Invoke-WebRequest : <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="https://test.safetylearning.co.uk/api/1.3/UserManagement"> <SOAP-ENV:Body><ns1:AddUserResponse><AddUserResult><errorcode>500</errorcode> <description>Error</description><errors><item><id>52001</id><description>Unable to add one or more users.</description></item><item><id>52010</id> <description>User with ID ['9991'] fails</description></item><item><id>0</id> <description>A user identified by ID ['9991'] exists</description></item> </errors></AddUserResult></ns1:AddUserResponse> </SOAP-ENV:Body></SOAP-ENV:Envelope> At C:\Temp\Delme3.ps1:37 char:18 + ... $response = Invoke-WebRequest -Uri $UserURI -Method POST -ContentType ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
So I can see the error I want i.e. A user identified by ID ['9991'] exists. However
$SOAPResponse.Envelope.Body.AddUserResponse.AddUserResult.errors
returns nothing!
$SOAPResponse.Envelope.Body.FirstChild.InnerXml
returns:
<AddUserResult><errorcode>200</errorcode><description>OK</description><errors /></AddUserResult>
<errors>
is empty!
SOAPUI gives the correct error:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="https://test.safetylearning.co.uk/api/1.3/UserManagement">
<SOAP-ENV:Body>
<ns1:AddUserResponse>
<AddUserResult>
<errorcode>500</errorcode>
<description>Error</description>
<errors>
<item>
<id>52001</id>
<description>Unable to add one or more users.</description>
</item>
<item>
<id>52010</id>
<description>User with ID ['9999'] fails</description>
</item>
<item>
<id>0</id>
<description>A user identified by ID ['9999'] exists</description>
</item>
</errors>
</AddUserResult>
</ns1:AddUserResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Upvotes: 2
Views: 1016
Reputation: 1
You need to wrap the request in a Try/Catch and reference the $error[0].ErrorDetails in the Catch block. That specific user error would be returned with the following Catch block:
try {
[xml]$response = Invoke-WebRequest -Uri $UserURI -Method POST -ContentType "test/xml;charset=utf-8" -Body $input
$SOAPResponse = [xml]$response.Content
$SOAPResponse
]
catch {
[xml]$errResponse = $error[0].ErrorDetails
$errCode = $errResponse.Envelope.body.AddUserResponse.AddUserResult.errors.ChildNodes[2].description
$errCode
}
That block would output the valid SOAP response, or just the error code buried in that SOAP envelope from the error
$errResponse.Envelope.body.AddUserResponse.AddUserResult.errors.ChildNodes[2].description
A user identified by ID ['9991'] exists
Upvotes: 0