Skip to content

Read the first value of a request header

HttpRequestExtensions.TryGetFirstHeader reads the first value of a request header without ever throwing.

Field Value
Operation ID OP-10
Trigger Compose the trace and log message, step 2, and any consumer call.
Inputs HttpRequestMessage, header key
Outputs string
Primary components HttpRequestExtensions.TryGetFirstHeader(this HttpRequestMessage, string)
Entry point src/SAIF.Web.API/HttpRequestExtensions.cs

Steps

  1. If request.Headers != null and request.Headers.TryGetValues(key, out values) succeeds, return values.FirstOrDefault() (BR-WEBAPI-019).
  2. May return null if the header exists with an empty value collection.*
  3. Otherwise return "" (BR-WEBAPI-019).
  4. Never throws on a missing header.

Process flow

flowchart TD
    Start(["Called from OP-05 step<br>2, or any consumer"]) --> HasHeaders{"Headers != null and<br>TryGetValues succeeds?"}
    HasHeaders -->|Yes| First["return<br>values.FirstOrDefault()"]
    HasHeaders -->|No| Empty["return \"\""]

Failure and edge-case behavior

  • Never throws; a header present with an empty value collection can yield null rather than "".*

Note: Statements marked with * were inferred from code structure, naming conventions, or contextual evidence where direct confirmation was not available in source artifacts.

Sources

  • src/SAIF.Web.API/HttpRequestExtensions.cs