$ show fsprojects/FsHttp
FsHttp is a .NET HTTP client library for F# (and C#) that provides a fluent, composable DSL for constructing and sending HTTP requests. It offers both a computation expression (CE) syntax for F# and extension methods for C#, supporting all standard HTTP methods, headers, cookies, multipart form data, file uploads, and various body content types (text, JSON, binary, streams, form-urlencoded). The library wraps System.Net.Http and provides rich response handling including streaming, JSON parsing (System.Text.Json by default, with optional FSharp.Data and Newtonsoft.Json integrations), XML parsing, file saving, and status code assertions. It features a configurable pipeline with transformers for headers, URLs, HttpClient, and HttpRequestMessage, as well as per-request and global configuration. The library is designed for both interactive use (F# Interactive / notebooks) and production applications, with built-in FSI printing and debug logging. The codebase is well-structured with clean domain models, a modular DSL implementation, and comprehensive test coverage using Suave for local test servers.
The LLM identified 4 files worth highlighting for reuse:
Contains reusable stream and async helper functions. The `Stream.readUtf8StringAsync` function correctly handles UTF-8 surrogate pairs when reading a stream up to a maximum number of code points, which is a non-trivial encoding concern. The `EnumerableStream` class wraps a `byte seq` as a `Stream`, enabling streaming of lazy sequences without buffering the entire data. The `Stream.copyToCallbackAsync` provides progress callbacks during stream copying. These utilities are self-contained and could be extracted into a separate project.
Provides structural JSON comparison with subset/exact modes and array order options. The `compareJson` function flattens JSON into path-value pairs for comparison, handling nested objects and arrays. The `expectJson`/`assertJson` functions allow flexible JSON assertions useful for testing HTTP APIs. This module is self-contained (depends only on FSharp.Data.JsonValue) and could be reused in any F# project needing JSON diffing.
Defines the core domain types for HTTP requests and responses using immutable F# records. The type hierarchy (HeaderContext, BodyContext, MultipartContext) with interfaces IRequestContext, IUpdateConfig, IUpdatePrintHint, IToBodyContext, IToMultipartContext enables a type-safe builder pattern. The separation of Config, PrintHint, and the transformer lists demonstrates a clean composable architecture. This domain model is the foundation of the library's flexibility and could serve as a reference for designing similar DSLs.
Implements the pipeline-style DSL functions for constructing HTTP requests. Provides modules for HttpMethods, HeaderContext, Header, Body, Multipart, Config, and Print, each with well-named functions that operate on context types. The `HeaderContext.setUrl` method handles URL splitting and comment removal. The `Config.transformHeader`/`transformUrl`/`useBaseUrl` functions enable composable request pre-configuration. This module is the primary API surface and demonstrates a clean functional design.