Tcl
Tcl
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 Tcl Downloads
load ./chilkat.dll
set success 0
set rest [new_CkRest]
set bTls 1
set bAutoReconnect 1
set success [CkRest_Connect $rest "example.com" 443 $bTls $bAutoReconnect]
if {$success == 0} then {
puts [CkRest_lastErrorText $rest]
delete_CkRest $rest
exit
}
# Normally you would not hard-code secrets in source. You should instead obtain them from an
# interactive prompt, environment variable, or a secrets vault.
set username "myUsername"
set password "myPassword"
# Configure HTTP Basic authentication. Chilkat automatically adds the Authorization header to
# each request sent through this object.
set success [CkRest_SetAuthBasic $rest $username $password]
if {$success == 0} then {
puts [CkRest_lastErrorText $rest]
delete_CkRest $rest
exit
}
set responseText [CkRest_fullRequestNoBody $rest "GET" "/api/protected"]
if {[CkRest_get_LastMethodSuccess $rest] == 0} then {
puts [CkRest_lastErrorText $rest]
delete_CkRest $rest
exit
}
puts "$responseText"
delete_CkRest $rest