如何获取 Restheart 的 Etag collection

How to get Etag for Restheart collection

我正在尝试使用 Restheart API 删除 collection。

$http DELETE 127.0.0.1:8080/testDB/testCollection

但我收到错误消息:

"The collection's ETag must be provided using the 'If-Match' header."

如果我使用 GET:

http GET 127.0.0.1:8080/testDB/testCollection

我可以从最后一个 GET 请求响应中看到 etag 并将其手动添加到 If-Match header 以删除 collection.

但是我不明白如何检索给定 collection(即 testCollection)的 _etag。

我的最终目标是从使用 apache http commons 作为 REST API 客户端的 java 应用程序中删除 collection。因此,欢迎使用 java 中的示例。

只需 GET 127.0.0.1:8080/testDB/testCollection?pagesize=0 即可获得 etag,您会在 Etag 响应中的属性 之间找到它 header

http -a a:a 127.0.0.1:8080/db/coll?pagesize=0
HTTP/1.1 200 OK
...
ETag: 58653f6b2d174c09c590262a**

{
    "_embedded": [], 
    "_etag": {
        "$oid": "58653f6b2d174c09c590262a"
    }, 
    "_id": "coll", 
    "_returned": 0,
}

另请注意,尝试删除 collection returns Etag 响应 header 以防发生冲突

http -a a:a DELETE 127.0.0.1:8080/db/coll
HTTP/1.1 409 Conflict
...
ETag: 58653f6b2d174c09c590262a

{
    "http status code": 409, 
    "http status description": "Conflict", 
    "message": "The collection's ETag must be provided using the 'If-Match' header."
}

终于可以在配置文件中设置Etag校验行为了。默认是仅在 DELETE /db 和 /coll 上检查 etag,但可以启用任何写入请求(例如避免所谓的幽灵写入问题)

来自 conf 文件:

#### ETag policy

# the following configuration defines the default etag check policy
# the policy applies for dbs, collections (also applies to file buckets) and documents
# valid values are REQUIRED, REQUIRED_FOR_DELETE, OPTIONAL

etag-check-policy:
    db: REQUIRED_FOR_DELETE
    coll: REQUIRED_FOR_DELETE
    doc: OPTIONAL