Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66286

DocumentDB - DELETE causes 401 error

I always get 401 when I try to execute a DELETE verb against a DocumentDB collection. POST for queries/inserts/updates works fine, so I am sure that my code that calculates the authorization token is correct. What I am not sure however is what I should pass for the resource id and resource type.

payLoad := LowerCase(Verb) + #10+
           LowerCase(resourceType) + #10+
           LowerCase(resourceId) + #10+
           LowerCase(utc_date) + #10 +
           '' + #10; //instead of the "date" header

For the POST (which works), I use

"docs" and "dbs/mydb/mycol/licenses"

For DELETE I use the value of the "_self" property for the url of the doc to be deleted. I tried just about any combination of resource type and resource id I could think of:

docs    dbs/icUoAA==/colls/icUoAKizCgA=/docs
docs    dbs/icUoAA==/colls/icUoAKizCgA=/docs/icUoAKizCgADAAAAAAAAAA==
docs    dbs/icUoAA==/colls/icUoAKizCgA=/docs/icUoAKizCgADAAAAAAAAAA==/
docs/icUoAKizCgADAAAAAAAAAA==   dbs/icUoAA==/colls/icUoAKizCgA=/docs
docs/icUoAKizCgADAAAAAAAAAA==   dbs/icUoAA==/colls/icUoAKizCgA=/docs/icUoAKizCgADAAAAAAAAAA==
etc.

DELETE however always fails with 401. Do I need to use something different?

Thank you!

Upvotes: 1

Views: 498

Answers (1)

Shreshth
Shreshth

Reputation: 160

There's two kinds of requests you can make to DocumentDB. One is based on the "id" property that you define, and one based on the internal resource id ("_rid"). When you're using "id", you need to use the entire link for generating the authorization token, so for delete it would look like

delete
docs
dbs/mydb/colls/mycol/docs/cc0d4caabc714aac976c9d0a2e9243ed
sun, 21 aug 2016 20:00:00
 //newline

If you're using the internal resource ID, then you just pass in the "_rid" of the document you're trying to delete, for example

delete
docs
icuoakizcgadaaaaaaaaaa== //lowercase
sun, 21 aug 2016 20:00:00
 //newline

One thing which might help debugging these is looking at the response body that's returned with the 401. It should contain an error message telling you the payload that the server expected.

Upvotes: 2

Related Questions