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 <C_CkRestW.h>
#include <C_CkSecureStringW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRestW rest;
    BOOL bTls;
    BOOL bAutoReconnect;
    HCkSecureStringW ssUsername;
    HCkSecureStringW ssPassword;
    const wchar_t *responseText;

    success = FALSE;

    rest = CkRestW_Create();
    bTls = TRUE;
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"example.com",443,bTls,bAutoReconnect);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        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.
    ssUsername = CkSecureStringW_Create();
    CkSecureStringW_Append(ssUsername,L"myUsername");
    ssPassword = CkSecureStringW_Create();
    CkSecureStringW_Append(ssPassword,L"myPassword");

    success = CkRestW_SetAuthBasicSecure(rest,ssUsername,ssPassword);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkSecureStringW_Dispose(ssUsername);
        CkSecureStringW_Dispose(ssPassword);
        return;
    }

    responseText = CkRestW_fullRequestNoBody(rest,L"GET",L"/api/protected");
    if (CkRestW_getLastMethodSuccess(rest) == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkSecureStringW_Dispose(ssUsername);
        CkSecureStringW_Dispose(ssPassword);
        return;
    }

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


    CkRestW_Dispose(rest);
    CkSecureStringW_Dispose(ssUsername);
    CkSecureStringW_Dispose(ssPassword);

    }