5.16Setting Request Content (Body)
Updated on Aug 20, 2026~13 min read
Supports setting any type of request content.
HTTP declarative requests configure request content via the BodyAttribute attribute. The corresponding HTTP declarative extractor is the BodyDeclarativeExtractor type, which is responsible for parsing a single BodyAttribute attribute and building the request content configuration required by the HttpRequestBuilder instance.
public interface IHttpService : IHttpDeclarative{ // Marks the parameter as the request content [Post("https://furion.net/")] Task<string> PostStringAsync([Body] object body); // Automatically infers Content-Type // Supports setting Content-Type [Post("https://furion.net/")] Task<string> PostStringAsync([Body("application/json")] object body); // or use [Body(MediaTypeNames.Application.Json)] // Supports setting Content-Type and character set [Post("https://furion.net/")] Task<string> PostStringAsync([Body("application/json; charset=utf-8")] object body); // URL-encoded form [Post("https://furion.net/")] Task<string> PostStringAsync([Body("application/x-www-form-urlencoded")] object body); // Uses StringContent to build the URL-encoded form [Post("https://furion.net/")] Task<string> PostStringAsync([Body("application/x-www-form-urlencoded", UseStringContent = true)] object body); // Can be configured to skip URL encoding [Post("https://furion.net/")] Task<string> PostStringAsync([Body("application/x-www-form-urlencoded", urlEncode = false)] object body); // Supports raw string content [Post("https://furion.net/")] Task<string> PostStringAsync([Body(RawString = true)] string body); // Default content type text/plain [Post("https://furion.net/")] Task<string> PostStringAsync([Body("application/json", RawString = true)] string body); // Supports configuring a file path (or internet address) [Post("https://furion.net/")] Task<string> PostStringAsync([Body(AsFile = true)] string filePath); // Frozen parameter types will be ignored [Post("https://furion.net/")] Task<string> PostStringAsync([Body] CancellationToken cancellationToken);}Parameters marked with BodyAttribute are set through the underlying httpRequestBuilder.SetContent method, and any non-frozen parameter type is supported.
BodyAttribute contains the following constructors and properties:
-
Constructors:
new(): applies to a parameter, treating the parameter as the request content.new(contentType): applies to a parameter, treating the parameter as the request content, and supports setting the content type.new(contentType, contentEncoding): applies to a parameter, treating the parameter as the request content, and supports setting the content type and encoding.
-
Properties:
ContentType: the content type (of typestring).ContentEncoding: the content encoding (of typestring).UseStringContent: whether to useStringContentto buildFormUrlEncodedContent, defaulting tofalse, and only effective whenContentTypeisapplication/x-www-form-urlencoded.UrlEncode: whether toURL-encode the form data (of typebool), defaulting totrue.RawString: whether the content is a raw string (of typebool), defaulting tofalse, and only effective when the parameter is a string type and this property istrue.AsFile: treats the string as a file path (internet addresses supported) (of typebool), defaulting tofalse, and only effective when the parameter is a string type and this property istrue.DisposeResourcesOnRequestCompletion: whether to automatically release resources after the request completes (of typebool), defaulting tofalse.