Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkRest.pb"
IncludeFile "CkSecureString.pb"

Procedure ChilkatExample()

    success.i = 0

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    bTls.i = 1
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"example.com",443,bTls,bAutoReconnect)
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ;  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.i = CkSecureString::ckCreate()
    If ssUsername.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkSecureString::ckAppend(ssUsername,"myUsername")
    ssPassword.i = CkSecureString::ckCreate()
    If ssPassword.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkSecureString::ckAppend(ssPassword,"myPassword")

    success = CkRest::ckSetAuthBasicSecure(rest,ssUsername,ssPassword)
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkSecureString::ckDispose(ssUsername)
        CkSecureString::ckDispose(ssPassword)
        ProcedureReturn
    EndIf

    responseText.s = CkRest::ckFullRequestNoBody(rest,"GET","/api/protected")
    If CkRest::ckLastMethodSuccess(rest) = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkSecureString::ckDispose(ssUsername)
        CkSecureString::ckDispose(ssPassword)
        ProcedureReturn
    EndIf

    Debug responseText


    CkRest::ckDispose(rest)
    CkSecureString::ckDispose(ssUsername)
    CkSecureString::ckDispose(ssPassword)


    ProcedureReturn
EndProcedure