-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Open
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationEarly API idea and discussion, it is NOT ready for implementationarea-System.Net.Http
Milestone
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationEarly API idea and discussion, it is NOT ready for implementationarea-System.Net.Http