[API Proposal]: Adding headers with a method

Open
#108,148 8 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
api

Research direction

Start with HttpClient.DefaultRequestHeaders and the proposed HttpClientExtensions.AddHeader signature. Review the requested dictionary-based convenience API, including its return type, null handling, duplicate-header behavior, and naming. Done means an agreed API design with clear acceptance criteria.

Written by the indexing model from the issue text.

Description

api-suggestion area-System.Net.Http
Background and motivation

Firstly when I had many headers to add for my httpclient defaultHeader, I was adding them in seprate lines, then I looked for a method and I did not find that so I created a method that takes a Dictionary of string and in that method by foreach I added the keys and values to default header.
This approach made my code cleaner.
@karelz

API Proposal
namespace System.Net.Http.System.Net.Http
{
    public static class HttpClientExtensions
    {

        public static Task AddHeader(this HttpClient httpclinet, Dictionary<string, string> dictionary)
        {
            if (dictionary == null)
                return;
            foreach (var item in dictionary)
            {
                if (!client.DefaultRequestHeaders.Contains(header.Key))
                {
                    httpclinet.DefaultRequestHeaders.Add(item.Key, item.Value);
                }
            }
            return Task.CompletedTask;


        }
    }
}
API Usage
using(var client = new HttpClient()){
var headers = new Dictionary<string,string>(){
{"X-Authorization":"abcd"},
{"X-gateway","Payment"}
};
client.AddHeader(headers);
client.post();
}
Alternative Designs

No response

Risks

No response

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.