TclHTTPS: Difference between revisions

From EggWiki
Jump to navigation Jump to search
(Created page with "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 run...")
 
No edit summary
Line 4: Line 4:


To connect to a webpage and display its contents, you can use the following code example:
To connect to a webpage and display its contents, you can use the following code example:
<code>
 
<pre>
package require http
package require http
package require tls
package require tls
Line 16: Line 17:
}
}
putlog [http::data $req]
putlog [http::data $req]
</code>
</pre>

Revision as of 22:24, 5 February 2023

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]