Sample code for 30+ languages & platforms
Xbase++

Configure HTTP Basic Authentication for REST

See more REST Examples

Demonstrates Rest.SetAuthBasic, which configures HTTP Basic authentication from a username and password. Chilkat automatically adds the Authorization header to each request sent through the object.

Background. HTTP Basic authentication transmits credentials with every request, so it should be used over HTTPS. Credentials should be obtained from a secure source rather than hard-coded in production code.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRest
LOCAL nBTls
LOCAL nBAutoReconnect
LOCAL cUsername
LOCAL cPassword
LOCAL cResponseText

nSuccess := 0

oRest := CreateObject("Chilkat.Rest")
nBTls := 1
nBAutoReconnect := 1
nSuccess := oRest:Connect("example.com", 443, nBTls, nBAutoReconnect)
IF (nSuccess == 0)
    ? oRest:LastErrorText
    oRest:destroy()
    RETURN
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.
cUsername := "myUsername"
cPassword := "myPassword"

//  Configure HTTP Basic authentication.  Chilkat automatically adds the Authorization header to
//  each request sent through this object.
nSuccess := oRest:SetAuthBasic(cUsername, cPassword)
IF (nSuccess == 0)
    ? oRest:LastErrorText
    oRest:destroy()
    RETURN
ENDIF

cResponseText := oRest:FullRequestNoBody("GET", "/api/protected")
IF (oRest:LastMethodSuccess == 0)
    ? oRest:LastErrorText
    oRest:destroy()
    RETURN
ENDIF

? cResponseText

oRest:destroy()