MandM
MandM

Reputation: 3383

Azure CosmosDB REST error creating document

I'm getting the following error when trying to create a document using the REST API:

The input content is invalid because the required properties - 'id; ' - are missing

In the various SDKs there is an optional parameter to include, disableAutomaticIdGeneration, which if set to true will reject a request with this error if an ID is not supplied. I'm making a pretty bare REST request, so I'm not adding this parameter myself (and I don't even know what header -- presumably a header -- I would add because the documentation for the REST API doesn't cover it).

The only extra thing to note is that the collection has a defined partition key. I can't find any documentation that says that an ID is required if a partition key is defined, but that appears to be the case.

Unless someone can answer as to why this is/could be happening, I'm going to consider my "it-must-be-defined-if-partition-key-is-defined" answer the answer eventually.

Request below (it's not the bare HTTP, but those are the contents):

{
    "headers": {
        "content-type": "application/json",
        "x-ms-version": "2017-02-22",
        "x-ms-date": "Mon, 25 Mar 2019 17:57:03 GMT"
    },
    "body": {
        "data": "myData",
        // "id": "some_id" //I don't want to do this because I want Cosmos to auto-generate this like when I create a document in the Portal!
    },
    "method": "post",
    "url": "https://my-database-server.documents.azure.com/dbs/MyDatabase/colls/MyCollection/docs"
}

A few more questions to untangle...

The Create a Document documentation states that id is Required, so maybe the Portal is actually creating it browser-side and then including it in the request under the hood, but if that was the case why does the disableAutomaticIdGeneration option exist in the SDKs?

Also, the same documentation states (highlighting is mine):

It is the unique ID that identifies the document, that is, no two documents should share the same id. The id must not exceed 255 characters. The ID field is automatically added when a document is created without specifying the ID value. However, you can always update the ID value by assigning a custom value to it in the request body.

By the bolded-section, it seems like the key id is required, but maybe I can pass a null or empty string? But, I tried both of those and got errors both times (one saying that null was not allowed and the other was that the string cannot be empty).

Upvotes: 4

Views: 3216

Answers (1)

Matias Quaranta
Matias Quaranta

Reputation: 15613

The current SDKs auto-generate missing ids as Guid values. The Azure Portal uses the JS SDK, that is why you see the Portal experience creating the id for you if you don't add it.

The documentation on the REST API seems misleading and needs to be adjusted from what you share, since the REST API won't autogenerate the id, it will validate that it's in the payload though.

In summary, if you are implementing your own REST client, you can add your own autogenerate logic client-side (like the SDKs do), the REST API requisite is that it's included in the payload.

Upvotes: 6

Related Questions