question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using DefaultAzureCredential results in HTTP Error 400. The size of the request headers is too long

See original GitHub issue

Describe 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:closed
  • Created 8 months ago
  • Reactions:1
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
vihanv01commented, Feb 8, 2023

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?

0reactions
ealsurcommented, Mar 27, 2023

Please reactivate once questions are answered

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot "request header too large" or "bad request"
The "Request header too large" message is thrown with an HTTP error code 400. This error occurs if the size of the request...
Read more >
HTTP Error 400. The Size of the Request Headers Is Too ...
The HTTP error 400. the size of the request headers is too long. C# is a standard Google Chrome error when the user...
Read more >
http 400: size of header request is too long when signing in ...
The error HTTP 400: Size of header request is too long generally happens because there's too many cookies. Azure AD B2C's login goes...
Read more >
HTTP Error 400. The size of the request headers is too long.
This error generally occurs when there's too many cookies or cookies that are too big. Each time you are signing in, it's storing...
Read more >
How to Fix a 400 Bad Request Error: 8 Easy Methods
Stuck with a 400 bad request error? Read this article to find out 8 simple methods to solve the problem and its possible...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found