Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Boolean iBTls
    Boolean iBAutoReconnect
    Variant vSsUsername
    Handle hoSsUsername
    Variant vSsPassword
    Handle hoSsPassword
    String sResponseText
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End
    Move True To iBTls
    Move True To iBAutoReconnect
    Get ComConnect Of hoRest "example.com" 443 iBTls iBAutoReconnect To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Get Create (RefClass(cComChilkatSecureString)) To hoSsUsername
    If (Not(IsComObjectCreated(hoSsUsername))) Begin
        Send CreateComObject of hoSsUsername
    End
    Get ComAppend Of hoSsUsername "myUsername" To iSuccess
    Get Create (RefClass(cComChilkatSecureString)) To hoSsPassword
    If (Not(IsComObjectCreated(hoSsPassword))) Begin
        Send CreateComObject of hoSsPassword
    End
    Get ComAppend Of hoSsPassword "myPassword" To iSuccess

    Get pvComObject of hoSsUsername to vSsUsername
    Get pvComObject of hoSsPassword to vSsPassword
    Get ComSetAuthBasicSecure Of hoRest vSsUsername vSsPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComFullRequestNoBody Of hoRest "GET" "/api/protected" To sResponseText
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sResponseText


End_Procedure