IssueRaiser

[API] Add per request headers override to HttpClient convenience methods

## Motivation Custom per request http headers importance are on the rise due to their role in authentication and authentication in newer api security models. The way to send custom per request headers with `HttpClient` is via `SendAsync` and adding them to the `HttpRequestMessage`. However that isn't very discoverable and is a big jump from using the convenience methods `GetAsync`, `PostAsync`, etc This can lead people to changing the `DefaultRequestHeaders` on the `HttpClient` instead: [SO](https://stackoverflow.com/a/35910012/1109918), [SO](https://stackoverflow.com/q/42156953/1109918), [SO](https://stackoverflow.com/q/10679214/1109918), etc Which is very dangerous if the `HttpClient` is used as a static; which is [also an official suggested practice](https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client) > **HttpClient** is intended to be instantiated once and re-used throughout the life of an application. Especially in server applications, creating a new **HttpClient** instance for every request will exhaust the number of sockets available under heavy loads. This will result in **SocketException** errors. And in popular blogs ["You're using HttpClient wrong and it is destabilizing your software"](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/) > This is dramatic. If you have any kind of load at all you need to remember these two things: > > 1. Make your HttpClient static. > 2. Do not dispose of or wr

Posted by benaadams on Aug 25, 2017, 4:14 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments

github (SaaS)

[API] Add per request headers override to HttpClient convenience methods

benaadamsAug 25, 2017, 4:14 AM

## Motivation Custom per request http headers importance are on the rise due to their role in authentication and authentication in newer api security models. The way to send custom per request headers with `HttpClient` is via `SendAsync` and adding them to the `HttpRequestMessage`. However that isn't very discoverable and is a big jump from using the convenience methods `GetAsync`, `PostAsync`, etc This can lead people to changing the `DefaultRequestHeaders` on the `HttpClient` instead: [SO](https://stackoverflow.com/a/35910012/1109918), [SO](https://stackoverflow.com/q/42156953/1109918), [SO](https://stackoverflow.com/q/10679214/1109918), etc Which is very dangerous if the `HttpClient` is used as a static; which is [also an official suggested practice](https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client) > **HttpClient** is intended to be instantiated once and re-used throughout the life of an application. Especially in server applications, creating a new **HttpClient** instance for every request will exhaust the number of sockets available under heavy loads. This will result in **SocketException** errors. And in popular blogs ["You're using HttpClient wrong and it is destabilizing your software"](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/) > This is dramatic. If you have any kind of load at all you need to remember these two things: > > 1. Make your HttpClient static. > 2. Do not dispose of or wr

Comments (0)

No comments yet. Start the discussion.

Comment on this issue