Configs2026年5月23日·1 分钟阅读

RestSharp — Simple REST API Client for .NET

A mature .NET HTTP client library that simplifies REST API consumption with automatic serialization, authentication helpers, and a fluent request builder.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Stage only · 29/100Stage only
Agent 入口
任意 MCP/CLI Agent
类型
CLI Tool
安装
Single
信任
信任等级:Established
入口
RestSharp Overview
通用 CLI 安装命令
npx tokrepo install a0b1ba08-56c4-11f1-9bc6-00163e2b0d79

Introduction

RestSharp is a lightweight HTTP client library for .NET that wraps HttpClient with a higher-level API. It handles JSON and XML serialization, authentication, parameter binding, and file uploads, reducing the boilerplate required when calling REST APIs.

What RestSharp Does

  • Sends GET, POST, PUT, PATCH, DELETE requests with typed responses
  • Serializes and deserializes JSON and XML automatically
  • Supports OAuth1, OAuth2, JWT, and basic authentication
  • Handles file uploads and multipart form data
  • Provides URL segment, query parameter, and header binding

Architecture Overview

RestSharp v107+ is built on top of HttpClient and HttpRequestMessage, acting as a convenience wrapper rather than a standalone HTTP stack. The RestClient manages an HttpClient instance and applies serializers, authenticators, and interceptors to each request. Serialization is pluggable, with System.Text.Json as the default and Newtonsoft.Json available as an add-on.

Self-Hosting & Configuration

  • Install via NuGet: dotnet add package RestSharp
  • For Newtonsoft.Json: dotnet add package RestSharp.Serializers.NewtonsoftJson
  • Create one RestClient per base URL and reuse it (it wraps HttpClient)
  • Configure default headers, authenticators, and base URL in the constructor
  • Compatible with .NET 6 through .NET 9

Key Features

  • Fluent request builder with strong typing for parameters
  • Built-in authenticators for OAuth1, OAuth2, and JWT bearer tokens
  • Interceptor pipeline for modifying requests and responses
  • Automatic content-type negotiation and deserialization
  • Download-to-stream support for large file responses

Comparison with Similar Tools

  • HttpClient (raw) — built into .NET; lower-level, requires manual serialization
  • Refit — interface-based REST client with source generation; less flexibility, more type safety
  • Flurl — fluent URL builder and HTTP client; similar convenience, different API style
  • WebRequest (legacy) — deprecated .NET API; should not be used in new code
  • RestEase — interface-driven REST client; lighter than Refit, smaller community

FAQ

Q: Should I create one RestClient per request? A: No. RestClient wraps HttpClient internally and should be reused per base URL. Creating a new instance per request can lead to socket exhaustion.

Q: How do I handle API rate limiting? A: Use RestSharp interceptors to inspect response headers for rate-limit data and implement backoff logic, or combine with Polly for retry policies.

Q: Can RestSharp upload files? A: Yes. Use request.AddFile("field", filePath) to attach files as multipart form data.

Q: What changed in RestSharp v107? A: RestSharp v107 was a major rewrite that moved from a custom HTTP stack to wrapping HttpClient. It dropped sync methods, simplified the API, and made the library lighter.

Sources

讨论

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

相关资产