Sample code for 30+ languages & platforms
Unicode C++

Configure HTTP Basic Authentication with SecureString

See more REST Examples

Demonstrates Rest.SetAuthBasicSecure, which configures HTTP Basic authentication using SecureString objects instead of plaintext strings.

Background. A SecureString keeps sensitive values encrypted in memory, reducing the window in which credentials appear as readable plaintext. This is otherwise equivalent to SetAuthBasic.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkRestW.h>
#include <CkSecureStringW.h>

void ChilkatSample(void)
    {
    bool success = false;

    CkRestW rest;
    bool bTls = true;
    bool bAutoReconnect = true;
    success = rest.Connect(L"example.com",443,bTls,bAutoReconnect);
    if (success == false) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    //  SetAuthBasicSecure configures HTTP Basic authentication using SecureString objects, which keep
    //  the credentials encrypted in memory.
    //  Normally you would not hard-code secrets in source.  You should instead obtain them from an
    //  interactive prompt, environment variable, or a secrets vault.
    CkSecureStringW ssUsername;
    ssUsername.Append(L"myUsername");
    CkSecureStringW ssPassword;
    ssPassword.Append(L"myPassword");

    success = rest.SetAuthBasicSecure(ssUsername,ssPassword);
    if (success == false) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    const wchar_t *responseText = rest.fullRequestNoBody(L"GET",L"/api/protected");
    if (rest.get_LastMethodSuccess() == false) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    wprintf(L"%s\n",responseText);
    }