I am using a static HttpClient instance in an Azure Function app. I then add Auth headers on every request to the Http trigger.
I am currently doing this on every request
httpClient.DefaultRequestHeaders.Clear();
Then I add my other headers, could this cause issues? Or is it best practice to clear them before adding them in case they already exist?
CodePudding user response:
You shouldn't use the DefaultRequestHeaders
of HttpClient
if that instance is possibly shared by multiple invocations of the Azure Function and the value of the headers changes per request. In this scenario you should use the Headers
property of the HttpRequestMessage
class.
Using the DefaultRequestHeaders
of HttpClient
is problematic because multiple invocations can change the headers so you cannot rely on the httpclient sending the right header because another invocation of the function can have changed the headers in its own way.