Sample code for 30+ languages & platforms
PureBasic

Configure AWS Signature Authentication for REST

See more REST Examples

Demonstrates Rest.SetAuthAws, which associates an AuthAws provider so Chilkat signs each request using AWS Signature Version 4. The provider supplies the access key, secret key, region, and service name.

Background. AWS requires each request to be signed with a signature derived from the secret key, region, service, and request contents. Configuring the provider lets Chilkat compute and attach the signature automatically.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkRest.pb"
IncludeFile "CkAuthAws.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

    ;  Configure AWS Signature Version 4 request signing.  Provide the access key, secret key, region,
    ;  and service name.
    authAws.i = CkAuthAws::ckCreate()
    If authAws.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Normally you would not hard-code secrets in source.  You should instead obtain them from an
    ;  interactive prompt, environment variable, or a secrets vault.
    CkAuthAws::setCkAccessKey(authAws, "AWS_ACCESS_KEY")
    CkAuthAws::setCkSecretKey(authAws, "AWS_SECRET_KEY")
    CkAuthAws::setCkRegion(authAws, "us-east-1")
    CkAuthAws::setCkServiceName(authAws, "s3")

    ;  Associate the provider so Chilkat signs each request according to AWS requirements.
    success = CkRest::ckSetAuthAws(rest,authAws)
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        ProcedureReturn
    EndIf

    responseText.s = CkRest::ckFullRequestNoBody(rest,"GET","/")
    If CkRest::ckLastMethodSuccess(rest) = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        ProcedureReturn
    EndIf

    Debug responseText


    CkRest::ckDispose(rest)
    CkAuthAws::ckDispose(authAws)


    ProcedureReturn
EndProcedure