使用TCL Rest软件包进行基本身份验证的问题

问题描述 投票:0回答:1

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 rest模块中的基本身份验证凭据有关。

rest tcl basic-authentication
1个回答
0
投票

[感谢Shawn的评论指出,我误读了TCL文档中?的含义。带问号的参数surrounded是可选的,而不是带问号的参数。我将::rest::simple url query ?config? ?body?解释为查询参数是可选的。如果没有查询,则可以使用空查询作为必需参数。最终工作:

set response [rest::simple $url {} {
    method get
    auth {basic user password}
    format json
    }]
© www.soinside.com 2019 - 2024. All rights reserved.