Response status code does not indicate success when inserting documents
Asked Answered
V

4

9

Have have following partion id on my container: /vesselId

I am trying to add a collection of this object:

public class CoachVessel
{
    [JsonProperty("id")]
    public string vesselId { get; set; }

    [JsonProperty("imo")]
    public long Imo { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }
}

This is my code to bulk insert the documents:

 CosmosClientOptions options = new CosmosClientOptions() { AllowBulkExecution = true };
 CosmosClient cosmosclient = new CosmosClient(connStr, options);
 Container container = cosmosclient.GetContainer("CoachAPI", "Vessels");
    
 List<Task> concurrentTasks = new List<Task>();
 foreach (var vessel in vessels.Take(1))
 {
     concurrentTasks.Add(container.CreateItemAsync(vessel, new PartitionKey(vessel.vesselId)));
 }        
 await Task.WhenAll(concurrentTasks);

I get following error that does not provide much information?

Microsoft.Azure.Cosmos.CosmosException: 'Response status code does not indicate success: BadRequest (400); Substatus: 1001; ActivityId: ; Reason: ();'

Any pointers to what causes this? This is my settings: enter image description here

I have same problem when deleting documents:

CosmosClientOptions options = new CosmosClientOptions() { AllowBulkExecution = true,  MaxRetryAttemptsOnRateLimitedRequests=1000};
CosmosClient cosmosclient = new CosmosClient(connStr, options);
Container container = cosmosclient.GetContainer("CoachAPI", "Vessels");


var allItemsQuery = container.GetItemQueryIterator<string>("SELECT * FROM c.id");

List<Task> concurrentDeleteTasks = new List<Task>();

while (allItemsQuery.HasMoreResults)
{
    foreach (var item in await allItemsQuery.ReadNextAsync())
    {
        concurrentDeleteTasks.Add(container.DeleteItemAsync<string>(item, new PartitionKey("id")));
    }
}
            
await Task.WhenAll(concurrentDeleteTasks.Take(3));

Throws following error: 'Response status code does not indicate success: NotFound (404); Substatus: 0;

Vertigo answered 8/7, 2020 at 14:46 Comment(1)
Seeing this as well, loving the empty error message... </s>Selfregard
P
5

The partition key must match a property in the document body. Change the partition key for the container to be, /id and fix your deletion code to correctly specify the partition key. E.g.,

concurrentDeleteTasks.Add(container.DeleteItemAsync<string>(item, new PartitionKey(item.Id)));
Pradeep answered 12/7, 2020 at 7:46 Comment(0)
H
3

In my case the below error was because I'd updated the .Net SDK from v2 to v3, which no longer auto-generates IDs if one isn't passed.

Microsoft.Azure.Cosmos.CosmosException: 'Response status code does not indicate success: BadRequest (400); Substatus: 1001; ActivityId: ; Reason: ();'

I use the repository pattern, so just added a check before calling CreateItemAsync:

if (item.Id == null)
{
    item.Id = Guid.NewGuid().ToString();
}
Homoeroticism answered 27/11, 2020 at 12:28 Comment(0)
C
1

I'm from the CosmosDB Engineering Team. From your question, you've defined the partition key on the container as /vesselId, whereas the document has mapped the vesselId to the "id" property in the CoachVessel class. Is this intentional?

If the optional PartitionKey is specified in the CreateItemAsync API, then it needs to match the partition key in the Document. If you intended "id" to be the partition key, then you need to define your container's partition key as "id", not "vesselId". In this case, if the container's partition key is indeed /vesselId, the code expects a property "vesselId" in the input document set to the value vessel.vesselId specified in the partition key. It looks like the "vesselId" property is missing in your input document.

Crymotherapy answered 8/7, 2020 at 23:22 Comment(4)
Thanks for your response. I actually started that way but literately tried all options. As we speak my partion key is /id. I have tried with concurrentTasks.Add(container.AddItemAsync(vessel, new PartitionKey("/id")));, concurrentTasks.Add(container.AddItemAsync(vessel, new PartitionKey("/d")));,concurrentTasks.Add(container.AddItemAsync(vessel, new PartitionKey("/vesselId"))); and concurrentTasks.Add(container.AddItemAsync(vessel, new PartitionKey("vesselId"))); All give same error. If I dont provide the partionkey i get no errors.Vertigo
Btw notice this is my first cosmos db app I may have missed something very obvious.Vertigo
You don't pass the path of the partition key you pass the value.Arman
I am trying to save document using Azxure Function and I get this error "Executed 'Functions.Function' (Failed, Id=6fe6c66d-07ee-41d9-af24-aa308aa9bcb4, Duration=2591ms) [2023-07-19T06:56:19.547Z] System.Private.CoreLib: Exception while executing function: Functions.Function. Microsoft.Azure.Cosmos.Client: Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 5b313f49-3328-415e-9e5a-183a33940ad4; Reason: ({"Errors":["One of the specified inputs is invalid"]}"Puparium
P
0

In my case I was not sending Id in my model, once I started sending Id error went away.

Prosaic answered 14/8 at 17:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.