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

标签 javascript soap salesforce postman

我正在尝试在 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><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="037b7b7b7b7b7b7b7b7b7b437b7b7b7b7b2d606c6e" rel="noreferrer noopener nofollow">[email protected]</a></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/

相关文章:

php - 修复 HTML Validator 抛出的警告

javascript - JQuery 将 onclick 字符串添加到不属于某个类的 anchor

javascript - 使用 Javascript 和 Ajax 的 Rails 开发环境

azure - 如何使用逻辑应用程序中的连接器连接到 Salesforce 的自定义对象

c# - 如何通过iframe获取另一个页面的asp隐藏字段值

c# - SoapExtension System.Configuration.ConfigurationErrorsException in web.config 无法解析属性 'type'的值

web-services - axis2 网络服务中的返回列表

java - Spring WS/启动: How to set WebServiceTemplate property in client interceptor?

jquery - 未捕获的类型错误 : Cannot call method 'noConflict' of undefined

java - 子对象更新时 Salesforce 更新主记录