Skip to content

[API Proposal]: Adding headers with a method #108148

@BehtashShirzad

Description

@BehtashShirzad

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions