TclHTTPS

From EggWiki
Revision as of 22:24, 5 February 2023 by Geo (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The http package is a common package required by Tcl scripts that need to connect to websites. However, the "default" commands don't make it easy to understand how to connect to an https website instead of an http website, and will often generate an error if the user tries to do so. Below is an example of how to do so.

NOTE: You have to install both the http AND tls Tcl libraries on the host for this to work- on Debian-based systems, this is done by an administrator running sudo apt-get install tcl-tls. The http package comes standard with Tcl; you shouldn't need to install it separately.

To connect to a webpage and display its contents, you can use the following code example:

package require http
package require tls

http::register https 443 [list ::tls::socket -autoservername true]
catch {set req [http::geturl https://my.secure.site/ -timeout 10000]} error    
set status [http::status $req]
if {$status != "ok"} {
  putlog "HTTP request error: $error"
  return
}
putlog [http::data $req]