Magento Postman Soap Login get sesssionId in Pre-request Script

I can use pm.sendRequest to make an XML request.

For example with magento to use soap I have to login to receive an id session

url magento request soap
http://127.0.0.1/magento-1.9.3.7/api/v2_soap

request

    <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
        <Body>
            <login xmlns="urn:Magento">
                <username>xxxx</username>
                <apiKey>xxxxx</apiKey>
            </login>
        </Body>
    </Envelope>

response

<SOAP-ENV:Body>
    <ns1:loginResponse>
        <loginReturn xsi:type="xsd:string">8292295363d53a2ca209dc4a8457da77</loginReturn>
    </ns1:loginResponse>
</SOAP-ENV:Body>

can I create a pre-request script using pm.sendRequest, pars the answer and just take the session id (loginReturn) to insert in another request (doing it all in one step)?

In Tests tab

var responseJson = xml2Json(responseBody);
postman.setEnvironmentVariable("sessionId", responseJson['SOAP-ENV:Envelope']['SOAP-ENV:Body']['ns1:loginResponse']['loginReturn']['_']);
  • It will set sessionId variable within environment and you can use it for other calls {{sessionId}}