Lua
Lua
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 Lua Downloads
local chilkat = require("chilkat")
local rest = chilkat.Rest.new()
local bTls = true
local bAutoReconnect = true
local success = rest:connect("example.com", 443, bTls, bAutoReconnect)
if not success then
print(rest.last_error_text)
return
end
-- Normally you would not hard-code secrets in source. You should instead obtain them from an
-- interactive prompt, environment variable, or a secrets vault.
local username = "myUsername"
local password = "myPassword"
-- Configure HTTP Basic authentication. Chilkat automatically adds the Authorization header to
-- each request sent through this object.
success = rest:set_auth_basic(username, password)
if not success then
print(rest.last_error_text)
return
end
local responseText = rest:full_request_no_body("GET", "/api/protected")
if not rest.last_method_success then
print(rest.last_error_text)
return
end
print(responseText)