Using DefaultAzureCredential results in HTTP Error 400. The size of the request headers is too long
See original GitHub issueDescribe the bug
When subsequently calling ReadNextAsync
on a FeedIterator
using a CosmosClient
created using a DefaultAzureCredential
, an exception is thrown stating that the request headers are too long.
This does not happen when using an authentication key. We want to move away from key-based access to Azure AD auth if possible.
Following the advice in this article, setting the ResponseContinuationTokenLimitInKb
property on QueryRequestOptions
has no effect and direct connection mode is not an option at the moment.
To Reproduce Here is a simple console app that reproduces the error:
using Azure.Identity;
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json.Linq;
internal static class Program
{
private static async Task Main()
{
var client = new CosmosClient(
accountEndpoint:"https://account-endpoint.documents.azure.com:443/",
tokenCredential: new DefaultAzureCredential(),
clientOptions: new CosmosClientOptions
{
ConnectionMode = ConnectionMode.Gateway,
AllowBulkExecution = true,
});
client.ClientOptions.RequestTimeout = TimeSpan.FromMinutes(5);
var container = client.GetContainer("database", "container");
var query = new QueryDefinition("SELECT * FROM c WHERE c.id = c.PartitionKey");
var iterator = container.GetItemQueryIterator<JObject>(query, requestOptions: new QueryRequestOptions()
{
ResponseContinuationTokenLimitInKb = int.MaxValue,
});
while (iterator.HasMoreResults)
{
var response = await iterator.ReadNextAsync();
Console.WriteLine($"Document Count: {response.Count} | Request Charge: {response.RequestCharge}");
}
}
}
The error occurs when calling ReadNextAsync
on line 30 after the first iteration of the loop.
When changing the parameter on line 11 to pass in an authentication key instead of the DefaultAzureCredential
, the query is able to be continued as usual.
Expected behaviour A successful continuation response.
Actual behaviour A CosmosException is thrown due to a Bad Request - Request Too Long response. Full exception message under additional context.
Environment summary SDK Version: 3.31.2 OS Version: Windows Cosmos Instance Default Consistency: Session
Additional context
Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: d4df1ca3-837a-48c3-be43-cfaff624b282; Reason: (Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: d4df1ca3-837a-48c3-be43-cfaff624b282; Reason: (Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: d4df1ca3-837a-48c3-be43-cfaff624b282; Reason: (<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Request Too Long</h2>
<hr><p>HTTP Error 400. The size of the request headers is too long.</p>
</BODY></HTML>
RequestUri: https://account-endpoint.documents.azure.com/dbs/database/colls/container/docs;
RequestMethod: POST;
Header: Authorization Length: 6598;
Header: x-ms-max-item-count Length: 4;
Header: x-ms-session-token Length: 18;
Header: x-ms-continuation Length: 10415;
Header: x-ms-documentdb-partitionkeyrangeid Length: 1;
Header: x-ms-documentdb-populatequerymetrics Length: 4;
Header: x-ms-documentdb-responsecontinuationtokenlimitinkb Length: 10;
Header: x-ms-cosmos-sdk-supportedcapabilities Length: 1;
Header: x-ms-cosmos-correlated-activityid Length: 36;
Header: x-ms-documentdb-query-iscontinuationexpected Length: 5;
Header: x-ms-documentdb-isquery Length: 4;
Header: x-ms-documentdb-query-enablecrosspartition Length: 4;
Header: x-ms-activity-id Length: 36;
Header: Cache-Control Length: 8;
Header: User-Agent Length: 89;
Header: x-ms-version Length: 10;
Header: Accept Length: 16;
ActivityId: d4df1ca3-837a-48c3-be43-cfaff624b282, Request URI: /dbs/database/colls/container/docs, RequestStats: Microsoft.Azure.Cosmos.Tracing.TraceData.ClientSideRequestStatisticsTraceDatum, SDK: Windows/10.0.19044 cosmos-netstandard-sdk/3.29.4);););
Issue Analytics
- State:
- Created 8 months ago
- Reactions:1
- Comments:13 (8 by maintainers)
Top GitHub Comments
Kindly ignore my previous message because I noticed that I changed the query during my latest debugging session where I removed the where clause. After resetting I can consistently hit the error again and then by changing the
ResponseContinuationTokenLimitInKb
property to be sufficiently small (anything between 1 and 4 works).Is there some guidance on what this value should be considering that we have limited control over the size of the authentication token? What are the risks / impact of making this value 1kb whenever we use the iterator?
Please reactivate once questions are answered