I have a static httpclient common to requests, and I want to add one custom header to it.
httpClient.DefaultRequestHeaders.Add("customHeader", somevalue.ToString());
But I noticed that for each request, a value is added to this header, which I intend to replace with each request. I am trying to remove the header if it already exists and add it again, but it gives me errors in the load test .
if (httpClient.DefaultRequestHeaders.Contains("customHeader")) { httpClient.DefaultRequestHeaders.Remove("customHeader"); } httpClient.DefaultRequestHeaders.Add("customHeader",somevalue.ToString());
Errors -
System.ArgumentException: An item with the same key has already been added. System.InvalidOperationException: Collection was modified; enumeration operation may not execute. System.ArgumentNullException: Value cannot be null.
How can I update the value of the custom header for each request?
source share