Version 54 of TclCurl

Updated 2011-09-14 12:28:35 by Patrick

What: TclCurl: "curl" is a "command line tool for transferring files with URL syntax"

 Where:  http://62.42.230.162/andresgarci/tclcurl/english/index.html (for TclCurl)
         http://curl.haxx.se/ (for curl, libcurl)
 Description: Tcl binding for libcurl, a library for accessing internet
        resources of various types.  Supports ftp, telnet,
        ldap, dict, http, https protocols.
        libcurl is currently at version 7.19.6 and
        TclCurl is currently at version 7.19.6 .
 Updated: 09/2009
 Contact: mailto:fandom at telefonica.net (Andres Garcia)

More background is available in "curl Simplifies Web Retrieval" [L1 ].


So, how is TclCurl better than the bare http (or ftp) package? A few examples should help make that clear [ WWW::Search::* analogue; HotMail mass downloader]. But, wouldn't either of those examples be just as feasible to implement with the http package?

You cannot GET a https page if you are sitting behind a proxy using the bare http package. I haven't tried TclCurl yet. US


  • websearch, an initial WWW::Search like package (using the http package, though, not TclCurl)

Most of the time you are right, the http package is complete enough to satisfy most users, TclCurl does provide more features, but unless you need them, you'd better use the http package and forget about the lack of portability a C extension brings.

But as I said, TclCurl does have more features; for example, you can tunnel non-http request through a http proxy. I haven't tried it myself, but it should be something like:

 curl::transfer -url https://www.securedomain.com -file index.html \
        -httpproxytunnel 1 -proxy 192.168.0.1:80

I don't have a proxy to test this- could someone please check whether it works?

TclCurl supports http 1.1, including keep-alive and resumed transfers, though there is work by Pat Thoyts to add 1.1 support to a new version of the http package, which you can get at http://tclsoap.sourceforge.net/http.html

As for the rest of the TclCurl features, some would be trivial to implement in Tcl, like being able to follow redirections, some a little bit harder, like cookie support, but the principle is always the same: check the features you need and, if the http package offers them, use it, but if it doesn't, check TclCurl.

Apart from http(s), TclCurl also offers support for other protocols, like ldap, gopher, ftp, ... but I have never used the pure Tcl clients, so I have no idea how they compare.

Even though they have not been written with TclCurl in mind, you may want to read http://curl.haxx.se/docs/httpscripting.shtml and http://curl.haxx.se/libcurl/c/the-guide.html

They will give you an idea of what can be done using TclCurl.

Andres


I believe the TclCurl package would be more popular if there were more examples. Below is an example on how to manually package up a Soap XML envelope over http, set http headers, and parse the Soap response using regexp. Overall, TclCurl's performance seems very fast.

Scott Nichols scott dot nichols4 at comcast dot net

    if { [catch {

        package require TclCurl

        set soapEnv {
        <SOAP-ENV:Envelope 
          xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
          xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" 
          xmlns:xsd="http://www.w3.org/1999/XMLSchema"
        >
            <SOAP-ENV:Body>
            <ns1:getTemp xmlns:ns1="urn:xmethods-Temperature" 
                SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
              <zipcode xsi:type="xsd:string">11217</zipcode>
            </ns1:getTemp>
           </SOAP-ENV:Body>

        </SOAP-ENV:Envelope>}


        puts $soapEnv

        # Build Http Headers.
        set httpHeaders ""
        lappend httpHeaders "Content-Type: text/xml; charset=utf-8" 
        lappend httpHeaders "SoapAction: GetTemperature for zip"

        puts $httpHeaders

        set httpBody ""
        set response [::curl::transfer -url http://services.xmethods.net:80/soap/servlet/rpcrouter \
        -post 1 \
        -postfields $soapEnv \
        -httpheader $httpHeaders \
        -bodyvar httpBody]

        puts "Soap Response: $response"
        puts "Soap Body: $httpBody"

        set rExp {<return xsi:type="xsd:float">(.*?)</return>}

        set temp 0

        regexp $rExp $httpBody sMatch temp

        puts "Current Temp for zip 11217 is $temp degrees"

        puts "------------------------------------------------------------------------"

     } errorString] } {
        puts "Error: $errorString"
        set bSuccess 0
        puts "------------------------------------------------------------------------"
 }

Example procs to GET and POST ~ BvW

 proc http_get {url args} {
    set pairs {}
    foreach {name value} $args {
        lappend pairs "[curl::escape $name]=[curl::escape $value]"
    }
    append url ? [join $pairs &]

    set curlHandle [curl::init]
    $curlHandle configure -url $url -bodyvar html
    catch { $curlHandle perform } curlErrorNumber
    if { $curlErrorNumber != 0 } {
        error [curl::easystrerror $curlErrorNumber]
    }
    $curlHandle cleanup

    return $html
 }

 proc http_post {url args} {
    set pairs {}
    foreach {name value} $args {
        lappend pairs "[curl::escape $name]=[curl::escape $value]"
    }
    set data [join $pairs &]

    set curlHandle [curl::init]
    $curlHandle configure -url $url -bodyvar html -post 1 -postfields $data
    catch { $curlHandle perform } curlErrorNumber
    if { $curlErrorNumber != 0 } {
        error [curl::easystrerror $curlErrorNumber]
    }
    $curlHandle cleanup

    return $html
 }