package require TclCurl
proc httpBlobDelete {url} {
set curlHandle [curl::init]
$curlHandle configure -url $url -customrequest DELETE
catch { $curlHandle perform } curlErrorNumber
if { $curlErrorNumber != 0 } {
return -code error [curl::easystrerror $curlErrorNumber]
}
set responsecode [$curlHandle getinfo responsecode]
$curlHandle cleanup
if {$responsecode == 204} {
return -code ok
} elseif {$responsecode == 404} {
return -code error "Not Found"
} else {
return -code error "ERROR"
}
}
用來測試 CrateDB REST API 中 BLOB delete 的程式。
如果 TclCurl 要指定 DELETE request,使用 -customrequest 來指定。因為 CrateDB 會回傳 status code 來表示檔案已成功刪除 (204),或者是沒有這個檔案 (404),所以取得 responsecode 以後判斷。大致上是這樣。
更新:
CrateDB 加入 BLOB 資料使用 PUT,下面是測試的程式:
package require TclCurl
proc httpBlobPut {url data} {
set curlHandle [curl::init]
$curlHandle configure -url $url -customrequest PUT -postfields $data
catch { $curlHandle perform } curlErrorNumber
if { $curlErrorNumber != 0 } {
return -code error [curl::easystrerror $curlErrorNumber]
}
set responsecode [$curlHandle getinfo responsecode]
$curlHandle cleanup
if {$responsecode == 201} {
return -code ok
} elseif {$responsecode == 409} {
return -code error "Conflict"
} else {
return -code error "ERROR"
}
}
使用 GET 讀取內容:
proc httpBlobGet {url} {
set curlHandle [curl::init]
$curlHandle configure -url $url -bodyvar html
catch { $curlHandle perform } curlErrorNumber
if { $curlErrorNumber != 0 } {
return -code error [curl::easystrerror $curlErrorNumber]
}
set responsecode [$curlHandle getinfo responsecode]
$curlHandle cleanup
if {$responsecode == 200} {
return -code ok $html
} elseif {$responsecode == 404} {
return -code error "Not Found"
} else {
return -code error "ERROR"
}
}
沒有留言:
張貼留言