Skip to content

HttpHeaders Remove will throw for some header names #61968

Closed

Description

@MihaZupan

The current implementation of Remove will throw if the header name we try to remove is well-known and not a part of the _allowedHeaderTypes.
For example, this means that calling content.Headers.Remove("Accept-Encoding") will result in an exception being thrown.

The result is that users must be aware of which headers are well-known and content-specific as defined in KnownHeaders.cs, which is just an implementation detail.
For example, YARP is forced to keep such a list: _contentHeaders.

We should consider changing the implementation of Remove to return false in case a descriptor is not available for a given header name:

-public bool Remove(string name) => Remove(GetHeaderDescriptor(name));
+public bool Remove(string name) => TryGetHeaderDescriptor(name, out HeaderDescriptor descriptor) && Remove(descriptor);

This would match the pattern for how TryAddWithoutValidation is used - try on request.Headers and fallback to request.Content.Headers.

Risk

The current behavior of throwing is likely working to inform the user of the fact that header collections differ and not all headers are treated equally.
The scenario of removing a non-constant header name is less likely in regular applications.

Activity

  1. ghost added
    untriaged
    on Nov 23, 2021
  2. stephentoub commented on Nov 23, 2021

    @stephentoub
    Member

    This seems reasonable. It's surprising and inconsistent that this throws.

  3. karelz commented on Nov 23, 2021

    @karelz
    Member

    Triage:

    • We should look at YARP to see if there are other similar APIs that need adjustment for "transparent" header usages
    • [nice to have] We might want to look at how common is the pattern and if anyone relies on throwing (which would be in dev cycle)
    • In .NET bool-returning methods usually don't throw, so it is a reasonable breaking change -- risk of breaking is low

    Ultimately we need to decide if YARP is special case in this pattern or not.

  4. added
    enhancement
    and removed
    untriaged
    on Nov 23, 2021
  5. added this to the Future milestone on Nov 23, 2021
  6. MihaZupan commented on Mar 28, 2022

    @MihaZupan
    MemberAuthor

    Note: The same applies to HttpHeaders.Contains as well.
    With this one there is at least the workaround of using NonValidated.Contains which uses TryGetHeaderDescriptor as suggested above.

    HttpHeaders.Contains and Remove will also throw for invalid header names (invalid tokens).
    NonValidated.Contains can be used as a workaround there.

  7. added
    in-pr
    on Apr 20, 2026
  8. modified the milestones: Future, 11.0.0 on Apr 28, 2026
  9. locked and limited conversation to collaborators on May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-System.Net.Httpenhancementin-pr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions