5.40Getting the Request Builder or Request Message (Pre-flight Request)
Created on Aug 17, 2026~5 min read
In some cases, you may want to only obtain the HTTP request object itself without actually sending the request. For example: verifying in unit tests that the generated request matches expectations, or further manually modifying the request builder after obtaining it, or passing the request message to another system for execution.
To this end, when the return type of a method that sends an HTTP remote request is HttpRequestBuilder or HttpRequestMessage, the framework builds and returns that object directly, skipping the actual network transmission. The example is as follows:
public interface IHttpService : IHttpDeclarative{ // HttpRequestBuilder type, no request is sent (pre-flight request) [Get("https://furion.net/")] Task<HttpRequestBuilder> GetRequestBuilderAsync(); // HttpRequestMessage type, no request is sent (pre-flight request) [Get("https://furion.net/")] Task<HttpRequestMessage> GetRequestMessageAsync();}When called, the object is obtained directly:
// Get the builder; you can continue chained configuration and then send manuallyvar builder = await httpService.GetRequestBuilderAsync(); // No request is sentbuilder.WithHeader("X-Custom", "value");var httpResponseMessage = await httpRemoteService.SendAsync(builder); // Initiate the network request// Get HttpRequestMessage for assertions or external passingvar httpRequestMessage = await httpService.GetRequestMessageAsync(); // No request is sentAssert.Equal("https://furion.net/", httpRequestMessage.RequestUri?.ToString());Use Cases
- Pre-flight Check: Before formally sending, check whether the generated request object matches expectations. After confirming that the
URL, request headers,Tokeninjection, and so on are all correct, send it manually or continue processing. - Unit Testing: Without simulating a network environment, directly verify whether the generated
HttpRequestMessagecontains the correct parameters, headers, and authentication information. - Request Object Passing: Pass the constructed
HttpRequestMessageto other services, libraries, or processes for execution, achieving separation between request construction and request execution. - Hybrid Programming: First complete most of the configuration through the builder or declarative style (parameter mapping,
Tokeninjection, etc.), then obtain the builder for minor dynamic modifications before sending manually, balancing the simplicity of the declarative style with the flexibility of the imperative style.