最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to provide soap xml body in Pre-Request Script in Postman tool - Stack Overflow

programmeradmin6浏览0评论

I am trying to add a script in the Postman pre-request script which will return a token. Following is dummy code for the request. I am not able to set the soap xml in body of the request. Need help on this !

Postman Pre-Request Script:

pm.sendRequest({
    url: ".0",
    method: "POST",
    header: {
        'Content-Type': 'text/xml',
        'SOAPAction': ''
      },
    body: {}
},
function (err, res) {
    console.log("Response - " + res);
}
);

Below mentioned is the XML to be set as body in above request:

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

Following is what i have tried ,

REQUEST:

pm.sendRequest({
    url: ".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=\"/\" xmlns:ns2=\"urn:enterprise.soap.sforce\"><SOAP-ENV:Header><ns2:Header></ns2:Header></SOAP-ENV:Header><SOAP-ENV:Body><ns3:login xmlns:ns3=\"urn:enterprise.soap.sforce\" 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);
}
);

RESPONSE:

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="/" xmlns:sf="urn:fault.enterprise.soap.sforce" xmlns:xsi=""><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>

I am trying to add a script in the Postman pre-request script which will return a token. Following is dummy code for the request. I am not able to set the soap xml in body of the request. Need help on this !

Postman Pre-Request Script:

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

Below mentioned is the XML to be set as body in above request:

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

Following is what i have tried ,

REQUEST:

pm.sendRequest({
    url: "https://test.salesforce./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/soap/envelope/\" xmlns:ns2=\"urn:enterprise.soap.sforce.\"><SOAP-ENV:Header><ns2:Header></ns2:Header></SOAP-ENV:Header><SOAP-ENV:Body><ns3:login xmlns:ns3=\"urn:enterprise.soap.sforce.\" 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);
}
);

RESPONSE:

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap/soap/envelope/" xmlns:sf="urn:fault.enterprise.soap.sforce." xmlns:xsi="http://www.w3/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>
Share Improve this question edited Jun 14, 2018 at 13:49 Sameer Patil asked Jun 14, 2018 at 6:12 Sameer PatilSameer Patil 4613 silver badges10 bronze badges 2
  • why not use the body section of postman? – User4179525 Commented Jun 14, 2018 at 8:09
  • @Sathish This is a Pre-request Script which will get executed before main request for which body is already mentioned in body section. Pre-request script allows you to generate pre-requisites for that particular request. In this case it will help me to get authentication token required. – Sameer Patil Commented Jun 14, 2018 at 11:42
Add a ment  | 

2 Answers 2

Reset to default 4

So finally, with help of @Borys Fursov i got the solution for my problem. Following is the correct request -

pm.sendRequest({
    url: "https://test.salesforce./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/soap/envelope/\" xmlns:ns2=\"urn:enterprise.soap.sforce.\"><SOAP-ENV:Header><ns2:Header></ns2:Header></SOAP-ENV:Header><SOAP-ENV:Body><ns3:login xmlns:ns3=\"urn:enterprise.soap.sforce.\" xmlns=\"\"><ns3:username>[email protected]</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());
    }
}
);

Unfortunately i can not paste output due to sensitive information. Thanks !

You should give us more info, some your unsuccessful tries, etc. Hope it will be helpful:

pm.sendRequest({
    url: "https://test.salesforce./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/soap/envelope/" xmlns:ns2="urn:enterprise.soap.sforce.">
    <SOAP-ENV:Header>
        <ns2:Header>
        </ns2:Header>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns3:login xmlns:ns3="urn:enterprise.soap.sforce." 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);
}
);

Note, that use ` to represent raw string verbatim.

发布评论

评论列表(0)

  1. 暂无评论