首页 > 解决方案 > 使用 TCL Rest 包的基本身份验证问题

问题描述

TCL 新手,在使用::rest::simple url query ?config? ?body?命令时遇到问题 - 特别是让基本身份验证工作。此处给出的示例(https://core.tcl-lang.org/tcllib/doc/tcllib-1-18/embedded/www/tcllib/files/modules/rest/rest.html#section4)如下:

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
    }]

所以我的尝试是:

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

但是,当我尝试针对 GET 的模拟 API 端点运行上述内容时,我不断收到 401 错误:

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

我可以使用基本身份验证(也使用 Python)对同一端点发出 curl 请求,如果我在端点上禁用基本身份验证,这在 TCL 中工作得很好:

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

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

puts $response

所以这与 TCL 休息模块中的基本身份验证凭据有关。

标签: resttclbasic-authentication

解决方案


感谢 Shawn 的评论指出我误读了?TCL 文档中的含义。问号包围的参数是可选的,而不是问号后面的参数。我将其解释::rest::simple url query ?config? ?body?为查询参数是可选的。如果没有查询,可以使用空查询作为必填参数。这最终起作用了:

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

推荐阅读