SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
805 stars 171 forks source link

drop database example #33

Closed jgranduel closed 9 years ago

jgranduel commented 9 years ago

Hi,

thanks for this great project. I've been testing it following walkthrough and docs. I cannot make DELETE work with ETag. I've never used it, sorry! I did this (on Windows 7, httpie-0.9.2)

PS4 > http --auth a:a  PUT <HOST>/myfirstdb desc='this is my first db'
HTTP/1.1 201 Created
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Location, ETag, Auth-Token, Auth-Token-Valid-Until, Auth-Token-Location
Auth-Token: 8b3cb79e-039c-4a27-804f-92ab3e411d35
Auth-Token-Location: /_authtokens/a
Auth-Token-Valid-Until: 2015-05-22T07:39:28.623Z
Connection: keep-alive
Content-Length: 0
Date: Fri, 22 May 2015 07:24:29 GMT

PS4 > http --auth a:a GET <HOST>/myfirstdb
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Location, ETag, Auth-Token, Auth-Token-Valid-Until, Auth-Token-Location
Auth-Token: 8b3cb79e-039c-4a27-804f-92ab3e411d35
Auth-Token-Location: /_authtokens/a
Auth-Token-Valid-Until: 2015-05-22T07:39:37.989Z
Connection: keep-alive
Content-Encoding: gzip
Content-Length: 325
Content-Type: application/hal+json
Date: Fri, 22 May 2015 07:24:37 GMT

{
    "_created_on": "2015-05-22T07:24:28Z",
    "_db-props-cached": false,
    "_embedded": {},
    "_etag": {
        "$oid": "555ed9ac84aecf8786566459"
    },
    "_id": "myfirstdb",
    "_lastupdated_on": "2015-05-22T07:24:28Z",
    "_links": {
        "curies": [
            {
                "href": "www.restheart.org/docs/v0.10/#api-db-{rel}",
                "name": "rh"
            }
        ],
        "rh:paging": {
            "href": "/myfirstdb/{?page}{&pagesize}",
            "templated": true
        },
        "rh:root": {
            "href": "/"
        },
        "self": {
            "href": "/myfirstdb"
        }
    },
    "_returned": 0,
    "_size": 0,
    "_total_pages": 1,
    "_type": "DB",
    "desc": "this is my first db"
}

PS4 > http --auth a:a DELETE <HOST>/myfirstdb IF-MATCH:'"$oid": "555ed9ac84aecf8786566459"'
HTTP/1.1 412 Precondition Failed
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Location, ETag, Auth-Token, Auth-Token-Valid-Until, Auth-Token-Location
Auth-Token: 8b3cb79e-039c-4a27-804f-92ab3e411d35
Auth-Token-Location: /_authtokens/a
Auth-Token-Valid-Until: 2015-05-22T07:40:10.474Z
Connection: keep-alive
Content-Length: 0
Date: Fri, 22 May 2015 07:25:10 GMT    

I'm sure I don't use ETag IF-MATCH correctly. I tried other syntax which return 409 Conflict as expected. Should I use $oid? Could you tell me the value I should pass around? Could an example be given with curl (as a matter of fact, with PowerShell, I use Invoke-RestMethod with a different syntax).

Thanks a lot, Best regards jgr

mkjsix commented 9 years ago

Hi, You just need to remove other characters from the If-Match part, like this: If-Match:555ed9ac84aecf8786566459

jgranduel commented 9 years ago

Thank you very much for your answer. It was that simple indeed! So here's the synopsis with HTTPIE along with PowerShell/Invoke-RestMethod

# CRUD DB with HTTPIE
http --auth a:a PUT <HOST>/myfirstdb desc='this is my first db'
http --auth a:a GET <HOST>/myfirstdb
http --auth a:a DELETE <HOST>/myfirstdb IF-MATCH:555ee63584aecf878656645d

# CRUD DB with PowerShell/Invoke-RestMethod
$c = Get-Credential #(a : a)
Invoke-RestMethod -Method Get -Credential $c -Uri "<HOST>/myfirstdb" -ErrorVariable err
$err.message | ConvertFrom-Json

Invoke-RestMethod -Method Put -Credential $c -Uri "<HOST>/myfirstdb" -ErrorVariable err -Body (@{ description = "Description..."} | ConvertTo-Json) -ContentType "application/json"

Invoke-RestMethod -Method Get -Credential $c -Uri "<HOST>/myfirstdb" -ErrorVariable err -OutVariable response

$etagValue = $response._etag.'$oid'
Invoke-RestMethod -Method Delete -Credential $c -Uri "<HOST>/myfirstdb" -ErrorVariable err -Headers @{ 'IF-MATCH' = $etagValue }    

HTTPIE is really great with very clean responses. When response is successfull, I still don't know how to get it with PowerShell.

best regard, jgr

mkjsix commented 9 years ago

Hi, Do you mean you need to have the same syntax highlighting with Invoke-RestMethod as httpie? I guess you could just use httpie in place of Invoke-RestMethod in any PowersShell script, adjusting for the different syntax, of course. Unfortunately we aren't PowerShell users, but this looks like a more general question you might try to ask in places like Stackoverflow, I think. An alternative would be to create instead a Python script which uses httpie directly, I guess it shouldn't be too hard if you know a little of Python, but it depends on your use cases.

jgranduel commented 9 years ago

Hi,

Do you mean you need to have the same syntax highlighting with Invoke-RestMethod as httpie? no ! I was justing adding a comment in case someone is interested as it was what I was trying to use in first place. Most Rest examples out there use curl, then HTTPIE, and I mention Invoke-RestMethod. When in PowerShell on Windows, I'm trying to stick with this cmdlet (really great for autocompletion). Thanks again, ciao!