Chilkat Online Tools

unicodeCpp / Salesforce Platform APIs / SOAP Login

Back to Collection Items

#include <CkHttpW.h>
#include <CkXmlW.h>
#include <CkStringBuilderW.h>
#include <CkHttpResponseW.h>

void ChilkatSample(void)
    {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttpW http;
    bool success;

    // Use this online tool to generate code from sample XML: Generate Code to Create XML

    // The following XML is sent in the request body.

    // <?xml version="1.0" encoding="utf-8"?>
    // <env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    //     <env:Body>
    //         <n1:login xmlns:n1="urn:partner.soap.sforce.com">
    //             <n1:username><![CDATA[{{username}}]]></n1:username>
    //             <n1:password><![CDATA[{{password}}{{secretToken}}]]></n1:password>
    //         </n1:login>
    //     </env:Body>
    // </env:Envelope>
    // 

    CkXmlW xml;
    xml.put_Tag(L"env:Envelope");
    xml.AddAttribute(L"xmlns:xsd",L"http://www.w3.org/2001/XMLSchema");
    xml.AddAttribute(L"xmlns:xsi",L"http://www.w3.org/2001/XMLSchema-instance");
    xml.AddAttribute(L"xmlns:env",L"http://schemas.xmlsoap.org/soap/envelope/");
    xml.UpdateAttrAt(L"env:Body|n1:login",true,L"xmlns:n1",L"urn:partner.soap.sforce.com");
    xml.UpdateChildContent(L"env:Body|n1:login|n1:username",L"{{username}}");
    xml.UpdateChildContent(L"env:Body|n1:login|n1:password",L"{{password}}{{secretToken}}");

    http.SetRequestHeader(L"Content-Type",L"text/xml; charset=UTF-8");
    http.SetRequestHeader(L"SOAPAction",L"login");
    http.SetRequestHeader(L"Accept",L"text/xml");

    CkStringBuilderW sbRequestBody;
    xml.GetXmlSb(sbRequestBody);

    CkHttpResponseW resp;
    success = http.HttpSb(L"POST",L"https://login.salesforce.com{{site}}/services/Soap/u/{{version}}",sbRequestBody,L"utf-8",L"text/xml; charset=UTF-8",resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    CkStringBuilderW sbResponseBody;
    resp.GetBodySb(sbResponseBody);

    CkXmlW xmlResponse;
    xmlResponse.LoadSb(sbResponseBody,true);
    wprintf(L"%s\n",xmlResponse.getXml());

    // Sample XML response:
    // (Sample code for parsing the XML response is shown below)

    // <?xml version="1.0" encoding="UTF-8"?>
    // <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    //     <soapenv:Body>
    //         <loginResponse>
    //             <result>
    //                 <metadataServerUrl>https://pozil-dev-ed.my.salesforce.com/services/Soap/m/50.0/00D58000000arpq</metadataServerUrl>
    //                 <passwordExpired>false</passwordExpired>
    //                 <sandbox>false</sandbox>
    //                 <serverUrl>https://pozil-dev-ed.my.salesforce.com/services/Soap/u/50.0/00D58000000arpq</serverUrl>
    //                 <sessionId>SECRET_SESSION_ID_GOES_HERE</sessionId>
    //                 <userId>00558000000yFyDAAU</userId>
    //                 <userInfo>
    //                     <accessibilityMode>false</accessibilityMode>
    //                     <chatterExternal>false</chatterExternal>
    //                     <currencySymbol>€</currencySymbol>
    //                     <orgAttachmentFileSizeLimit>26214400</orgAttachmentFileSizeLimit>
    //                     <orgDefaultCurrencyIsoCode>EUR</orgDefaultCurrencyIsoCode>
    //                     <orgDefaultCurrencyLocale>fr_FR_EURO</orgDefaultCurrencyLocale>
    //                     <orgDisallowHtmlAttachments>false</orgDisallowHtmlAttachments>
    //                     <orgHasPersonAccounts>false</orgHasPersonAccounts>
    //                     <organizationId>00D58000000arpqEAA</organizationId>
    //                     <organizationMultiCurrency>false</organizationMultiCurrency>
    //                     <organizationName>pozil DE</organizationName>
    //                     <profileId>00e58000000wYTOAA2</profileId>
    //                     <roleId xsi:nil="true"/>
    //                     <sessionSecondsValid>7200</sessionSecondsValid>
    //                     <userDefaultCurrencyIsoCode xsi:nil="true"/>
    //                     <userEmail>demo@pozil.org</userEmail>
    //                     <userFullName>Philippe Ozil</userFullName>
    //                     <userId>00558000000yFyDAAU</userId>
    //                     <userLanguage>en_US</userLanguage>
    //                     <userLocale>fr_FR</userLocale>
    //                     <userName>demo@pozil.org</userName>
    //                     <userTimeZone>Europe/Paris</userTimeZone>
    //                     <userType>Standard</userType>
    //                     <userUiSkin>Theme3</userUiSkin>
    //                 </userInfo>
    //             </result>
    //         </loginResponse>
    //     </soapenv:Body>
    // </soapenv:Envelope>

    // Sample code for parsing the XML response...
    // Use this online tool to generate parsing code from sample XML: Generate XML Parsing Code

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    const wchar_t *soapenv_Envelope_xmlns_soapenv = xmlResponse.getAttrValue(L"xmlns:soapenv");
    const wchar_t *soapenv_Envelope_xmlns = xmlResponse.getAttrValue(L"xmlns");
    const wchar_t *soapenv_Envelope_xmlns_xsi = xmlResponse.getAttrValue(L"xmlns:xsi");
    const wchar_t *metadataServerUrl = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|metadataServerUrl");
    const wchar_t *passwordExpired = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|passwordExpired");
    const wchar_t *sandbox = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|sandbox");
    const wchar_t *serverUrl = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|serverUrl");
    const wchar_t *sessionId = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|sessionId");
    const wchar_t *userId = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userId");
    const wchar_t *accessibilityMode = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|accessibilityMode");
    const wchar_t *chatterExternal = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|chatterExternal");
    const wchar_t *currencySymbol = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|currencySymbol");
    int orgAttachmentFileSizeLimit = xmlResponse.GetChildIntValue(L"soapenv:Body|loginResponse|result|userInfo|orgAttachmentFileSizeLimit");
    const wchar_t *orgDefaultCurrencyIsoCode = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|orgDefaultCurrencyIsoCode");
    const wchar_t *orgDefaultCurrencyLocale = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|orgDefaultCurrencyLocale");
    const wchar_t *orgDisallowHtmlAttachments = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|orgDisallowHtmlAttachments");
    const wchar_t *orgHasPersonAccounts = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|orgHasPersonAccounts");
    const wchar_t *organizationId = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|organizationId");
    const wchar_t *organizationMultiCurrency = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|organizationMultiCurrency");
    const wchar_t *organizationName = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|organizationName");
    const wchar_t *profileId = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|profileId");
    const wchar_t *roleId_xsi_nil = xmlResponse.chilkatPath(L"soapenv:Body|loginResponse|result|userInfo|roleId|(xsi:nil)");
    int sessionSecondsValid = xmlResponse.GetChildIntValue(L"soapenv:Body|loginResponse|result|userInfo|sessionSecondsValid");
    const wchar_t *userDefaultCurrencyIsoCode_xsi_nil = xmlResponse.chilkatPath(L"soapenv:Body|loginResponse|result|userInfo|userDefaultCurrencyIsoCode|(xsi:nil)");
    const wchar_t *userEmail = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|userEmail");
    const wchar_t *userFullName = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|userFullName");
    userId = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|userId");
    const wchar_t *userLanguage = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|userLanguage");
    const wchar_t *userLocale = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|userLocale");
    const wchar_t *userName = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|userName");
    const wchar_t *userTimeZone = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|userTimeZone");
    const wchar_t *userType = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|userType");
    const wchar_t *userUiSkin = xmlResponse.getChildContent(L"soapenv:Body|loginResponse|result|userInfo|userUiSkin");
    }

Curl Command

curl -X POST
	-H "Content-Type: text/xml; charset=UTF-8"
	-H "SOAPAction: login"
	-H "Accept: text/xml"
	-d '<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  <env:Body>
    <n1:login xmlns:n1="urn:partner.soap.sforce.com">
      <n1:username><![CDATA[{{username}}]]></n1:username>
      <n1:password><![CDATA[{{password}}{{secretToken}}]]></n1:password>
    </n1:login>
  </env:Body>
</env:Envelope>'
https://login.salesforce.com{{site}}/services/Soap/u/{{version}}

Postman Collection Item JSON

{
  "name": "SOAP Login",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "const result = xml2Json(pm.response.text())[\"soapenv:Envelope\"][\"soapenv:Body\"].loginResponse.result;",
          "const url = result.serverUrl.split(\"/\");",
          "",
          "const context = pm.environment.name ? pm.environment : pm.collectionVariables;",
          "context.set(\"_accessToken\", result.sessionId);",
          "context.set(\"_endpoint\", url[0] + \"//\" + url[2]);",
          "context.set(\"_userId\", result.userId);",
          "context.set(\"_orgId\", result.userInfo.organizationId);"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "value": "text/xml; charset=UTF-8",
        "type": "text"
      },
      {
        "key": "SOAPAction",
        "value": "login",
        "type": "text"
      },
      {
        "key": "Accept",
        "value": "text/xml",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<env:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n    xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">\n  <env:Body>\n    <n1:login xmlns:n1=\"urn:partner.soap.sforce.com\">\n      <n1:username><![CDATA[{{username}}]]></n1:username>\n      <n1:password><![CDATA[{{password}}{{secretToken}}]]></n1:password>\n    </n1:login>\n  </env:Body>\n</env:Envelope>"
    },
    "url": {
      "raw": "{{url}}{{site}}/services/Soap/u/{{version}}",
      "host": [
        "{{url}}{{site}}"
      ],
      "path": [
        "services",
        "Soap",
        "u",
        "{{version}}"
      ]
    },
    "description": "Logs in to the login server and starts a client session.\n\nMake sure to set the `url`, `username`, `password` and `secretToken` environment variables before sending this request.\nOn success, the request will automatically collect your authentication token for later use."
  },
  "response": [
    {
      "name": "Successful SOAP Login",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "text/xml",
            "type": "text"
          },
          {
            "key": "SOAPAction",
            "value": "login",
            "type": "text"
          },
          {
            "key": "charset",
            "value": "UTF-8",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<env:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n    xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">\n  <env:Body>\n    <n1:login xmlns:n1=\"urn:partner.soap.sforce.com\">\n      <n1:username>demo@pozil.org</n1:username>\n      <n1:password>PASSWORD_GOES_HERE</n1:password>\n    </n1:login>\n  </env:Body>\n</env:Envelope>"
        },
        "url": {
          "raw": "https://login.salesforce.com/services/Soap/u/50.0",
          "protocol": "https",
          "host": [
            "login",
            "salesforce",
            "com"
          ],
          "path": [
            "services",
            "Soap",
            "u",
            "50.0"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "xml",
      "header": [
        {
          "key": "Date",
          "value": "Wed, 06 Jan 2021 12:47:25 GMT"
        },
        {
          "key": "X-B3-TraceId",
          "value": "facd9cec92dd7211"
        },
        {
          "key": "X-B3-SpanId",
          "value": "facd9cec92dd7211"
        },
        {
          "key": "X-B3-Sampled",
          "value": "0"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache,must-revalidate,max-age=0,no-store,private"
        },
        {
          "key": "Content-Type",
          "value": "text/xml; charset=utf-8"
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        }
      ],
      "cookie": [
      ],
      "body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns=\"urn:partner.soap.sforce.com\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n    <soapenv:Body>\n        <loginResponse>\n            <result>\n                <metadataServerUrl>https://pozil-dev-ed.my.salesforce.com/services/Soap/m/50.0/00D58000000arpq</metadataServerUrl>\n                <passwordExpired>false</passwordExpired>\n                <sandbox>false</sandbox>\n                <serverUrl>https://pozil-dev-ed.my.salesforce.com/services/Soap/u/50.0/00D58000000arpq</serverUrl>\n                <sessionId>SECRET_SESSION_ID_GOES_HERE</sessionId>\n                <userId>00558000000yFyDAAU</userId>\n                <userInfo>\n                    <accessibilityMode>false</accessibilityMode>\n                    <chatterExternal>false</chatterExternal>\n                    <currencySymbol>€</currencySymbol>\n                    <orgAttachmentFileSizeLimit>26214400</orgAttachmentFileSizeLimit>\n                    <orgDefaultCurrencyIsoCode>EUR</orgDefaultCurrencyIsoCode>\n                    <orgDefaultCurrencyLocale>fr_FR_EURO</orgDefaultCurrencyLocale>\n                    <orgDisallowHtmlAttachments>false</orgDisallowHtmlAttachments>\n                    <orgHasPersonAccounts>false</orgHasPersonAccounts>\n                    <organizationId>00D58000000arpqEAA</organizationId>\n                    <organizationMultiCurrency>false</organizationMultiCurrency>\n                    <organizationName>pozil DE</organizationName>\n                    <profileId>00e58000000wYTOAA2</profileId>\n                    <roleId xsi:nil=\"true\"/>\n                    <sessionSecondsValid>7200</sessionSecondsValid>\n                    <userDefaultCurrencyIsoCode xsi:nil=\"true\"/>\n                    <userEmail>demo@pozil.org</userEmail>\n                    <userFullName>Philippe Ozil</userFullName>\n                    <userId>00558000000yFyDAAU</userId>\n                    <userLanguage>en_US</userLanguage>\n                    <userLocale>fr_FR</userLocale>\n                    <userName>demo@pozil.org</userName>\n                    <userTimeZone>Europe/Paris</userTimeZone>\n                    <userType>Standard</userType>\n                    <userUiSkin>Theme3</userUiSkin>\n                </userInfo>\n            </result>\n        </loginResponse>\n    </soapenv:Body>\n</soapenv:Envelope>"
    }
  ]
}