Skip to main content

Command Palette

Search for a command to run...

401 Unauthorized error when using CosmosDB REST APIs

Published
1 min read
401 Unauthorized error when using CosmosDB REST APIs
A

Data Engineer at Media.Net | Sharing my learnings and experiences in tech.

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 or doc-name for this. So, the string used to generate auth token should be dbs/{db-name}/colls/{coll-name}/docs. And then it's simply a POST request.

  • In all other cases when you require a doc-id or doc-name, the string will be different. In case of doc-id, the string will be dbs/{db-name}/colls/{coll-name}/docs/{doc-id} whereas in the case of doc-name, the string will be doc-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.

Tech

Part 3 of 6

In this series, I'll be giving a brief introduction of different technical concepts that I learn, to the best of my understanding.

Up next

What is Database Indexing?

And when to use it?