Search code examples
resttclbasic-authentication

Issues with Basic Authentication Using TCL Rest Package


New to TCL and having an issue with using the ::rest::simple url query ?config? ?body? command - specifically getting basic authentication to work. The example given here (https://core.tcl-lang.org/tcllib/doc/tcllib-1-18/embedded/www/tcllib/files/modules/rest/rest.html#section4) is as follows:

set url   http://twitter.com/statuses/update.json
    set query [list status $text]
    set res [rest::simple $url $query {
        method post
        auth   {basic user password}
        format json
    }]

So my attempt is:

package require rest
package require json

set url http://0.0.0.0:5000/api/id

set response [rest::simple $url {
    method get
    auth {basic user password}
    format json
}]

puts $response

However, I keep getting a 401 error when I try and run the above against a mock API endpoint for GET:

"GET /api/id?auth=basic%20user%20password&method=get&format=json HTTP/1.1" 401 -

I can make a curl request against that same endpoint using basic auth (with Python as well), and if I disable basic auth on the endpoint this works just fine in TCL:

set url http://0.0.0.0:5000/api/id

set response [rest::simple $url {
    method get
    format json
}]

puts $response

So it's something to do with the basic auth credentials in the TCL rest module.


Solution

  • Thanks to Shawn's comment pointing out I was misreading the meaning of ? in TCL docs. Parameters surrounded by question marks are optional, rather than parameters followed by question marks. I was interpreting ::rest::simple url query ?config? ?body? as meaning the query param was optional. If there is no query, you can use an empty query as the required parameter. This ended up working:

    set response [rest::simple $url {} {
        method get
        auth {basic user password}
        format json
        }]