Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkRest.h>
#include <C_CkSecureString.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRest rest;
    BOOL bTls;
    BOOL bAutoReconnect;
    HCkSecureString ssUsername;
    HCkSecureString ssPassword;
    const char *responseText;

    success = FALSE;

    rest = CkRest_Create();
    bTls = TRUE;
    bAutoReconnect = TRUE;
    success = CkRest_Connect(rest,"example.com",443,bTls,bAutoReconnect);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_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 = CkSecureString_Create();
    CkSecureString_Append(ssUsername,"myUsername");
    ssPassword = CkSecureString_Create();
    CkSecureString_Append(ssPassword,"myPassword");

    success = CkRest_SetAuthBasicSecure(rest,ssUsername,ssPassword);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkSecureString_Dispose(ssUsername);
        CkSecureString_Dispose(ssPassword);
        return;
    }

    responseText = CkRest_fullRequestNoBody(rest,"GET","/api/protected");
    if (CkRest_getLastMethodSuccess(rest) == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkSecureString_Dispose(ssUsername);
        CkSecureString_Dispose(ssPassword);
        return;
    }

    printf("%s\n",responseText);


    CkRest_Dispose(rest);
    CkSecureString_Dispose(ssUsername);
    CkSecureString_Dispose(ssPassword);

    }