Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access the Cosmos DB Table Endpoint using oData

I am learning Cosmos DB and am walking through the following tutorial on the Microsoft Cosmos DB Documentation site:

https://learn.microsoft.com/en-us/azure/cosmos-db/tutorial-query-table

Unfortunately, I am stuck at the point where I am trying to access the entities I created. Anytime I run the GET request to receive the oData response, I receive the following error message:

{
  "code": "BadRequest",
  "message": "Request url is invalid.\r\nActivityId: e7df6ff2-eaea-4d10-8823-ff0cee3a62c2"
}

I have an endpoint that looks like this (note that I replace my actual Cosmos account with abc):

https://abc.documents.azure.com

I also tried using to start my oData queries:

https://abc.documents.azure.com:443

According to the tutorial, I should create a database, a table, and add three entities. I did that successfully using C# with the Table API's TableOperation.Insert() method. Since I can successfully create the table and entities using the Table API, I know the endpoint I am using is accurate and made sure I copied/pasted that element from the connection string and I know it matches that of the database in Azure.

Here is what the database looks like in Azure:

Database, Tables, and Entities configured Next, the tutorial has you run a query with the table endpoint. Here is where I start getting confused because I cannot find documentation that explains how to get the Table's endpoint.

Here is the example the tutorial shows: https://<mytableendpoint>/People(PartitionKey='Harp',RowKey='Walter')

So I figure, just try this: https://abc.documents.azure.com/People(PartitionKey='Harp',RowKey='Walter')

That did not work, so I figure maybe I also need to specify the database (TablesDB as you see in the picture above): https://abc.documents.azure.com/TablesDB/People(PartitionKey='Harp',RowKey='Walter')

That does not work either. As mentioned earlier in this post, I did try appending :443 to the end of all my test queries but the error response is still the same.

Right now, I am trying all this with Postman GETs.

Also, I did try getting a token like this in Postman: https://login.microsoftonline.com/{{tenant_id}}/oauth2/token

And then sending the GET request using the Authorization parameter and passing the token but still get the same error as listed above.

Do you know how I can properly construct the table endpoint to successfully send a request and receive the oData response?

Thank you.

like image 345
Bill Avatar asked Oct 29 '25 17:10

Bill


2 Answers

The endpoint used in the original question is a document endpoint that actually exposes our SQL API and not our Table API. Since Tables have gone GA we no longer advertise that endpoint, instead we advertise a table endpoint that supports the same REST API as Azure Table storage. You can look at cosmos db, generate authentication key on client for Azure Table endpoint for a detailed walk through of how to talk to the endpoint, what headers you need, etc.

Please note that the easiest way to talk to the endpoint is to just use one of the Table API SDK. https://learn.microsoft.com/en-us/azure/cosmos-db/table-sdk-dotnet points to our .NET SDK but if you look to the left on that page you will see in the table of contents links for our Java, Python and Node SDKs as well. Note that these are the same SDKs as used for Azure Table storage. And also note that you can submit OData queries using them.

like image 197
Yaron Y. Goland Avatar answered Oct 31 '25 07:10

Yaron Y. Goland


I am using this API to query an entity from table based on PartitionKey and RowKey, it works fine on my side, please refer to it.

Request and Response:

enter image description here

enter image description here

enter image description here

Entities in table:

enter image description here

like image 40
Fei Han Avatar answered Oct 31 '25 07:10

Fei Han



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!