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

时间:2018-06-14 06:12:54

标签: javascript soap salesforce postman

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

邮差预先请求脚本:

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);
}
);

下面提到的是在上述请求中设置为正文的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 个答案:

答案 0 :(得分: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 :(得分:1)

最后,在@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());
    }
}
);

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