6.4Custom Content Processor (e.g. Serialization)
Updated on Aug 20, 2026~3 min read
In specific scenarios, when the framework's built-in IHttpContentProcessor content processors cannot meet your needs, you can solve the problem by implementing a custom IHttpContentProcessor content processor.
If you want to replace the framework's default System.Text.Json serialization provider, for example by using Newtonsoft.Json to add specific serialization configuration options for the application/json content type, you can implement the IHttpContentProcessor interface to meet this custom requirement.
public class CustomStringContentProcessor : HttpContentProcessorBase{ public override bool CanProcess(HttpContentProcessorContext context) => context.ContentType == "application/json"; public override HttpContent? Process(HttpContentProcessorContext context) { if (TryProcess(context, out var httpContent)) { return httpContent; } var content = context.RawContent!.GetType().IsBasicType() || context.RawContent is JsonElement or JsonNode ? context.RawContent.ToString() : context.RawContent.ToJsonString(ResolveJsonSerializerOptions(context.HttpClientName)); var stringContent = new StringContent(content!, context.Encoding, new MediaTypeHeaderValue(context.ContentType) { CharSet = context.Encoding?.WebName ?? "utf-8" }); return stringContent; }}Next, you can apply the custom content processor in the following two ways:
- Per-request setting:
HttpRequestBuilder.Post("https://furion.net/") .AddHttpContentProcessors(() => [ new CustomStringContentProcessor() ]) .SetJsonContent(new { id = 1, name = "Furion" });- Global configuration:
In the Startup.cs or Program.cs file, configure and register the HttpRemote service to enable the custom content processor feature:
services.AddHttpRemote(builder =>{ builder.AddHttpContentProcessors(() => [ new CustomStringContentProcessor() ]);});