需要使用 Tcllib 的 base64 與 Tcl 內建的 http 套件。
package require http
package require base64
set username admin
set password mypasswd
proc geturl_auth {url username password} {
set auth "Basic [base64::encode $username:$password]"
set headerl [list Authorization $auth]
set tok [http::geturl $url -headers $headerl]
set res [http::data $tok]
http::cleanup $tok
return $res
}
puts [geturl_auth http://127.0.0.1:5984/ $username $password]
在這個例子中,username 和 password 是之前就使用 set 設定好的變數(要替換真正的使用者帳號和密碼)。
另外一個範例,使用 POST method 送出 _restart 要求給 CouchDB。
package require http
package require base64
set username admin
set password mypasswd
proc geturl_auth {url method username password} {
set auth "Basic [base64::encode $username:$password]"
set headerl [list Authorization $auth Content-Type "application/json"]
set tok [http::geturl $url -method $method -headers $headerl]
set res [http::data $tok]
http::cleanup $tok
return $res
}
puts [geturl_auth http://127.0.0.1:5984/_restart POST $username $password]
另外一個範例,使用 GET method 送出 _all_dbs 要求給 CouchDB,取得目前的資料庫列表。
package require http
package require base64
set username admin
set password mypasswd
proc geturl_auth {url method username password} {
set auth "Basic [base64::encode $username:$password]"
set headerl [list Authorization $auth Content-Type "application/json"]
set tok [http::geturl $url -method $method -headers $headerl]
set res [http::data $tok]
http::cleanup $tok
return $res
}
puts [geturl_auth http://127.0.0.1:5984/_all_dbs GET $username $password]
另外一個範例,使用 GET method 送出 _uuids 要求給 CouchDB,CouchDB 會回傳一個 UUID 值。
package require http
package require base64
set username admin
set password mypasswd
proc geturl_auth {url method username password} {
set auth "Basic [base64::encode $username:$password]"
set headerl [list Authorization $auth Content-Type "application/json"]
set tok [http::geturl $url -method $method -headers $headerl]
set res [http::data $tok]
http::cleanup $tok
return $res
}
puts [geturl_auth http://127.0.0.1:5984/_uuids GET $username $password]
另外一個範例,可以說是 CouchDB 的基本操作,使用 PUT method 送出要求,要求給 CouchDB 建立一個資料庫。
package require http
package require base64
set username admin
set password mypasswd
proc geturl_auth {url method username password} {
set auth "Basic [base64::encode $username:$password]"
set headerl [list Authorization $auth Content-Type "application/json"]
set tok [http::geturl $url -method $method -headers $headerl]
set res [http::data $tok]
http::cleanup $tok
return $res
}
puts [geturl_auth http://127.0.0.1:5984/albums PUT $username $password]
另外一個範例,一樣是 CouchDB 的基本操作,使用 DELETE method 送出要求,要求 CouchDB 刪除一個資料庫。
package require http
package require base64
set username admin
set password mypasswd
proc geturl_auth {url method username password} {
set auth "Basic [base64::encode $username:$password]"
set headerl [list Authorization $auth Content-Type "application/json"]
set tok [http::geturl $url -method $method -headers $headerl]
set res [http::data $tok]
http::cleanup $tok
return $res
}
puts [geturl_auth http://127.0.0.1:5984/albums DELETE $username $password]
沒有留言:
張貼留言