📎 Webclip
Header propagation using ASP.NET Core
The page explains a simple ASP.NET Core setup with two Web API projects. One API receives a request, sends a call to an external API, and returns the headers that the external API received. By default, a custom header like sample-header is not forwarded.
It then shows how to add Microsoft.AspNetCore.HeaderPropagation, register the headers to propagate, attach the propagation handler to the named HttpClient, and enable the middleware in Startup. After that, the custom header is forwarded to the external API, which can be useful when passing an authorization header from a backend call.
Reading notes#
- The article starts from a case where one REST API calls another REST API and needs to pass through selected HTTP headers.
- A manual approach is described first: read headers from
Request.Headersand add them to the outgoingHttpRequestMessage. - The suggested library is
Microsoft.AspNetCore.HeaderPropagation, which handles this scenario. - The example uses two Web API projects, one named
ExternalApiand one namedSimpleApi. ExternalApiexposes a GET endpoint that returnsRequest.Headersso the received headers can be inspected.SimpleApicreates anHttpClientnamedexternalapi-clientand calls/externalapion the external service.- Without extra configuration, the custom header
sample-headeris not propagated. - The package is added with
dotnet add SimpleApi.csproj package Microsoft.AspNetCore.HeaderPropagation --version 3.1.6. - In
ConfigureServices, the code registerssample-headerwithAddHeaderPropagationand adds.AddHeaderPropagation()to theHttpClientregistration. - In
Configure, the app enables the middleware withapp.UseHeaderPropagation(). - The article says the feature works only when both parts are configured: the headers to propagate and the handler on the
HttpClient. - The final test shows the custom header reaching the external API.
- The post mentions a useful case where a SPA sends an authorization token to a backend, and the backend must pass that token to an external API.
