Search code examples
javamongodbrestheart

How to get Etag for Restheart collection


I am trying to delete a collection using the Restheart API.

$http DELETE 127.0.0.1:8080/testDB/testCollection

but I get the error:

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

If I use GET:

http GET 127.0.0.1:8080/testDB/testCollection

I can see the etag from the last GET request response and add it manually to the If-Match header in order to delete the collection.

However I don't understand how i would retrieve the _etag for a given collection(namely testCollection).

My final goal is to delete the collection from a java application using apache http commons as REST API client. Therefore examples in java are most welcome.


Solution

  • to get the etag just GET 127.0.0.1:8080/testDB/testCollection?pagesize=0 and you'll find it between its properties and in the Etag response 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,
    }
    

    also note that trying to delete the collection returns the Etag response header in case of Conflict

    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."
    }
    

    finally you can set the Etag check behaviour in the configuration file. Default is to check the etag only on DELETE /db and /coll, but can be enabled to any write request (for instance to avoid the so called ghost writes issue)

    from the conf file:

    #### 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