我正在调用SOAP API。 PowerShell提供了一个错误,并且该错误包含了我期望的响应!而我发送响应的变量的内容缺少一些数据。
[xml]$response = Invoke-WebRequest -Uri $UserURI -Method POST -ContentType "test/xml;charset=utf-8" -Body $input
$SOAPResponse = [xml]$response.Content
PowerShell返回:
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
所以我可以看到我想要的错误,即存在一个由ID ['9991']标识的用户。但是
$SOAPResponse.Envelope.Body.AddUserResponse.AddUserResult.errors
什么也不返回!
$SOAPResponse.Envelope.Body.FirstChild.InnerXml
返回:
<AddUserResult><errorcode>200</errorcode><description>OK</description><errors /></AddUserResult>
<errors>
为空!
SOAPUI给出了正确的错误:
<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>
答案 0 :(得分:0)
您需要将请求包装在Try / Catch中,并在Catch块中引用$ error [0] .ErrorDetails。该特定的用户错误将通过以下Catch块返回:
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
}
该块将输出有效的SOAP响应,或者仅输出错误中隐藏在该SOAP信封中的错误代码
$errResponse.Envelope.body.AddUserResponse.AddUserResult.errors.ChildNodes[2].description
A user identified by ID ['9991'] exists