如何在邮递员工具的预请求脚本中提供 soap xml 正文



我正在尝试在邮递员预请求脚本中添加一个脚本,该脚本将返回一个令牌。以下是请求的虚拟代码。我无法在请求正文中设置 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>

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

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

您应该向我们提供更多信息,一些您不成功的尝试等。 希望对您有所帮助:

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

请注意,使用 ' 逐字表示原始字符串。

最新更新