【问题标题】:How to get Etag for Restheart collection如何获得用于 Restheart 收藏的 Etag
【发布时间】:2023-03-19 16:01:01
【问题描述】:

我正在尝试使用 Restheart API 删除一个集合。

$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 标头中以删除集合。

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

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

【问题讨论】:

    标签: java mongodb restheart


    【解决方案1】:

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

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

    还要注意,如果发生冲突,尝试删除集合会返回 Etag 响应标头

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-08
      • 2018-08-05
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多