Scripts2026年7月21日·1 分钟阅读

Flurl — Fluent URL Builder and HTTP Client for .NET

Flurl combines a chainable URL builder with a lightweight HTTP client layer, letting you construct URLs and make HTTP calls in a single fluent expression with built-in testing support.

Agent 就绪

这个资产会安全暂存

这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。

Stage only · 29/100策略:需暂存
Agent 入口
任意 MCP/CLI Agent
类型
CLI Tool
安装
Single
信任
信任等级:Established
入口
Flurl
安全暂存命令
npx -y tokrepo@latest install c07278a0-8500-11f1-9bc6-00163e2b0d79 --target codex

先暂存文件;激活前需要读取暂存 README 和安装计划。

Introduction

Flurl treats URLs as mutable objects and HTTP calls as natural extensions of URL construction. Instead of juggling UriBuilder, HttpClient, and StringContent separately, you chain everything into one readable expression. Its test infrastructure lets you fake HTTP responses without touching a real server.

What Flurl Does

  • Builds URLs fluently with path segments, query parameters, and fragment handling
  • Makes GET, POST, PUT, PATCH, and DELETE requests as extension methods on URL strings
  • Deserializes JSON, receives streams, and uploads multipart forms inline
  • Provides a fake HTTP layer for unit testing without mocking HttpClient
  • Manages HttpClient instances per host with automatic connection pooling

Architecture Overview

Flurl consists of two packages. Flurl (core) is a URL builder that extends string and Uri with chainable methods. Flurl.Http adds an HTTP layer built on HttpClient with a FlurlClient that manages per-host connection pooling via IFlurlClientCache. Configuration flows through a settings object at global, client, or per-request scope. The testing module provides HttpTest which intercepts requests and returns fake responses.

Self-Hosting & Configuration

  • Install Flurl.Http via NuGet (includes the core Flurl URL builder)
  • Configure defaults globally: FlurlHttp.Clients.WithDefaults(s => s.WithTimeout(30))
  • Register named or typed clients with IFlurlClientCache for DI-friendly usage
  • Override JSON serialization with System.Text.Json or Newtonsoft.Json
  • Use HttpTest in tests to intercept and assert HTTP calls without network access

Key Features

  • Chainable URL construction that reads like a sentence
  • Automatic JSON serialization and deserialization on requests and responses
  • Per-host HttpClient management to avoid socket exhaustion
  • Built-in testability: HttpTest intercepts all Flurl calls in its scope
  • Configurable retry, timeout, redirect, and error handling policies

Comparison with Similar Tools

  • HttpClient — low-level; Flurl wraps it with a fluent API and manages pooling for you
  • RestSharp — similar convenience but no URL builder; Flurl's fluent chaining is more concise
  • Refit — interface-to-API mapper; Flurl is better for ad-hoc or dynamic URL construction
  • HTTPX (Python) — Python's fluent HTTP client; Flurl brings the same ergonomics to .NET
  • axios (JS) — promise-based HTTP; Flurl's chaining achieves a similar feel in C#

FAQ

Q: Does Flurl manage HttpClient lifecycle automatically? A: Yes. Flurl pools HttpClient instances per host URL by default, preventing socket exhaustion without manual management.

Q: How do I test code that uses Flurl? A: Wrap your test in using var httpTest = new HttpTest(); and call httpTest.RespondWithJson(data). All Flurl calls within that scope are intercepted.

Q: Can I use Flurl with dependency injection? A: Yes. Register IFlurlClientCache as a singleton and inject named or typed IFlurlClient instances into your services.

Q: Does Flurl support file uploads? A: Yes. Use .PostMultipartAsync(mp => mp.AddFile("file", stream, "name.pdf")) for multipart form uploads.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产