首页 文章

如何在 Postman 工具的 Pre-Request Script 中提供 soap xml body

提问于
浏览
0

我试图在 Postman pre-request 脚本中添加一个脚本,该脚本将返回一个令牌。以下是请求的虚拟代码。我无法在请求的正文中设置 soap xml。需要帮助!

邮差 Pre-Request 脚本:

pm.sendRequest({
    url: "https://test.salesforce.com/services/Soap/c/42.0",
    method: "POST",
    header: {
        'Content-Type': 'text/xml',
        'SOAPAction': ''
      },
    body: {}
},
function (err, res) {
    console.log("Response - " + res);
}
);

下面提到的是要在上面的请求中设置为 body 的 XML:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="urn:enterprise.soap.sforce.com">
    <SOAP-ENV:Header>
        <ns2:Header>
        </ns2:Header>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns3:login xmlns:ns3="urn:enterprise.soap.sforce.com" xmlns="">
            <ns3:username>USERNAME</ns3:username>
            <ns3:password>PASSWORD</ns3:password>
        </ns3:login>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

以下是我的尝试,

请求:

pm.sendRequest({
    url: "https://test.salesforce.com/services/Soap/c/42.0",
    method: "POST",
    header: {
        'Content-Type': 'text/xml',
        "SOAPAction": ""
      },
    body: {
        mode:"xml",
        xml: "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns2=\"urn:enterprise.soap.sforce.com\"><SOAP-ENV:Header><ns2:Header></ns2:Header></SOAP-ENV:Header><SOAP-ENV:Body><ns3:login xmlns:ns3=\"urn:enterprise.soap.sforce.com\" xmlns=\"\"><ns3:username>USERNAME</ns3:username><ns3:password>PASSWORD</ns3:password></ns3:login></SOAP-ENV:Body></SOAP-ENV:Envelope>"
    }
},
function (err, res) {
    var jsonObject = xml2Json(res);
    console.log("Response - " + jsonObject);
    pm.globals.set("session_id", jsonObject.sessionId);
}
);

响应:

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="urn:fault.enterprise.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><soapenv:Fault><faultcode>UNKNOWN_EXCEPTION</faultcode><faultstring>UNKNOWN_EXCEPTION: Premature end of file.</faultstring><detail><sf:UnexpectedErrorFault xsi:type="sf:UnexpectedErrorFault"><sf:exceptionCode>UNKNOWN_EXCEPTION</sf:exceptionCode><sf:exceptionMessage>Premature end of file.</sf:exceptionMessage></sf:UnexpectedErrorFault></detail></soapenv:Fault></soapenv:Body></soapenv:Envelope>

2 回答

  • 1

    你应该给我们更多的信息,一些你不成功的尝试,等等。希望它会有所帮助:

    pm.sendRequest({
        url: "https://test.salesforce.com/services/Soap/c/42.0",
        method: "POST",
        header: {
            'Content-Type': 'text/xml',
            'SOAPAction': ''
          },
        body: {
            mode:"raw",
            raw: `<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="urn:enterprise.soap.sforce.com">
        <SOAP-ENV:Header>
            <ns2:Header>
            </ns2:Header>
        </SOAP-ENV:Header>
        <SOAP-ENV:Body>
            <ns3:login xmlns:ns3="urn:enterprise.soap.sforce.com" xmlns="">
                <ns3:username>USERNAME</ns3:username>
                <ns3:password>PASSWORD</ns3:password>
            </ns3:login>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>`
        }
    },
    function (err, res) {
        console.log("Response - " + res);
    }
    );
    

    注意,使用`来逐字表示原始字符串。

  • 1

    所以最后,在帮助 of @Borys Fursov 我得到了我的问题的解决方案。以下是正确的要求 -

    pm.sendRequest({
        url: "https://test.salesforce.com/services/Soap/c/42.0",
        method: "POST",
        header: {
            'Content-Type': 'text/xml',
            'SOAPAction': '""'
          },
        body: {
            mode:"raw",
            raw: "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns2=\"urn:enterprise.soap.sforce.com\"><SOAP-ENV:Header><ns2:Header></ns2:Header></SOAP-ENV:Header><SOAP-ENV:Body><ns3:login xmlns:ns3=\"urn:enterprise.soap.sforce.com\" xmlns=\"\"><ns3:username>xxxxxxxxxx@xxxxx.com</ns3:username><ns3:password>xxxxxxxxxxxxxxxxxxxx</ns3:password></ns3:login></SOAP-ENV:Body></SOAP-ENV:Envelope> "
        }
    },
    function (err, res) {
        if (res.code === 200) {
            // console.log("Response - " + res.text());
            var responseJson = xml2Json(res.text());
            var sessionId = responseJson['soapenv:Envelope']['soapenv:Body'].loginResponse.result.sessionId;
            console.log("Session id - " + sessionId);
            pm.globals.set("session_id", sessionId);
        } else {
            console.log("Response - " + res.code + " " + res.reason());
            console.log("Response - " + res.text());
        }
    }
    );
    

    不幸的是,由于敏感信息,我无法粘贴输出。谢谢 !

相关问题