0

I have a .Net Core application that uses System.Net.Http.HttpClient to send a request to a .Net Core service.

I send multiple values for a single header.

var message = new HttpRequestMessage();

message.Headers.Add("Header1", "value1");
message.Headers.Add("Header1", "value2");

await client.SendAsync(message);

At this point if I inspect message.Headers, I see that "Header1" is being tracked as two distinct strings.

However, in the middleware of my receiveing .Net Core service, the values of "Header1" are joined.

public async Task Invoke(HttpContext httpContext)
{
    var header1 = httpContext.Request.Headers["Header1"];
}

The header1 variable here is of StringValues type, but it is an array of 1 string, instead of the expected 2. The value of this single string is "value1, value2".

I have no way to distinguish between whether "value1, value2" was two headers that were joined together or if it was originally a single value that happened to be equal to this, therefore, I cannot effectively parse the header value properly.

Does anyone else see this behavior? I would expect the header1 StringValues to be an array with two elements that I can easily parse, not a single string.

6
  • I've observed this with query variables, so I expect the same for header values... so bind to List<String> and make sure to always send at least 2. (If only 1 value is sent I initialize another to -1 to signify empty... that's for a List<int> I bind to. For strings just set one to "" and make sure it doesn't convert to null... there's a setting for ConvertEmptyStringToNull) Commented Sep 19, 2024 at 17:17
  • There is a standard of sending multiple values to a single header, here is the question link stackoverflow.com/questions/3096888/…. It means in standard, "value1, value2" only represents for the multiple values, and the single value should not happened to be in this format. Commented Sep 20, 2024 at 10:01
  • @FengzhiZhou, if this is the case then I don't understand why MS defined interface IHeaderDictionary : IDictionary<string, StringValues>. Why expose headers as a StringValues if all incoming values are just going to be combined into a single string? Commented Sep 20, 2024 at 18:33
  • From my comprehension it is for the purpose of efficiency. You could check this doc for some reasons of the applying of StringValues : andrewlock.net/a-brief-look-at-stringvalues . It will guarantee the efficiency but cannot make a clear distinction in reverse, and that is why we need to comply with the norms in many cases. Commented Sep 26, 2024 at 9:44
  • @FengzhiZhou, that is an enlightening article, but I did try enumerating the StringValues and even StringValues.ToArray(), and neither one breaks the headers apart as is called out in the article, and each one returns only a single string with the values concatenated together. Either the author is missing something or MS missed the memo. Commented Sep 27, 2024 at 13:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.