gpt4 book ai didi

javascript - 如何在Postman工具的预请求脚本中提供soap xml主体

转载 作者:行者123 更新时间:2023-12-01 01:58:03 26 4
gpt4 key购买 nike

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

postman 预请求脚本:

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

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

关于javascript - 如何在Postman工具的预请求脚本中提供soap xml主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50850716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com