Since Cosmos DB provides REST APIs to query their data, the process needs to be handled carefully while generating the authentication token.
Though I won't go into the depth of creating an authentication token, if you're having this 401 Unauthorized error, there's a high chance that something is wrong with the string used to create the authentication token.
Let's look at the possible scenarios.
Create a new Document: As you can see from the documentation, we don't need any
doc-id
ordoc-name
for this. So, the string used to generate auth token should bedbs/{db-name}/colls/{coll-name}/docs
. And then it's simply a POST request.In all other cases when you require a
doc-id
ordoc-name
, the string will be different. In case ofdoc-id
, the string will bedbs/{db-name}/colls/{coll-name}/docs/{doc-id}
whereas in the case ofdoc-name
, the string will bedoc-name
only.
Note: For APIs that require doc-name
as mentioned in the documentation, you can also use doc-id
.
A more detailed solution can be found in this stack overflow answer.
I hope that it helps.