首页 > 解决方案 > PowerShell 错误包含完整的 SOAP 响应而不是内容

问题描述

我正在调用一个 SOAP API。PowerShell 给出错误,错误包含我期望的响应!而我发送响应的变量的内容缺少一些数据。

[xml]$response = Invoke-WebRequest -Uri $UserURI -Method POST -ContentType "test/xml;charset=utf-8" -Body $input

$SOAPResponse = [xml]$response.Content

PowerShell 正在返回:

调用 WebRequest : <?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:信封 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>错误</description><errors><item><id>52001</id><description>无法
添加一个或多个用户。</description></item><item><id>52010</id>
<description>ID 为 ['9991'] 的用户失败</description></item><item><id>0</id>
<description>存在由 ID ['9991'] 标识的用户</description></item>
</errors></AddUserResult></ns1:AddUserResponse>
</SOAP-ENV:Body></SOAP-ENV:Envelope>
在 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>

标签: powershellsoapinvoke-webrequest

解决方案


您需要将请求包装在 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

推荐阅读