request

package
v0.0.0-...-7086fdf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete[T any](c *Client, ctx context.Context, path string, opts ...Opt) (*T, error)

Delete 发送 DELETE 请求

func Get

func Get[T any](c *Client, ctx context.Context, path string, opts ...Opt) (*T, error)

Get 发送 GET 请求

func GetHeaderMap

func GetHeaderMap(header string) map[string]string

GetHeaderMap 解析 header 字符串为 map 格式: "Key1=Value1\nKey2=Value2"

func Post

func Post[T any](c *Client, ctx context.Context, path string, body any, opts ...Opt) (*T, error)

Post 发送 POST 请求

func PostURL

func PostURL[T any](ctx context.Context, rawURL string, body any, opts ...Opt) (*T, error)

PostURL 向完整 URL 发送 POST 请求(无需 Client 实例)

func Put

func Put[T any](c *Client, ctx context.Context, path string, body any, opts ...Opt) (*T, error)

Put 发送 PUT 请求

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client HTTP 客户端

func NewClient

func NewClient(scheme string, host string, timeout time.Duration, opts ...ReqOpt) *Client

NewClient 创建 HTTP 客户端

func (*Client) GetHost

func (c *Client) GetHost() string

GetHost 获取主机地址

func (*Client) GetScheme

func (c *Client) GetScheme() string

GetScheme 获取协议

func (*Client) SetDebug

func (c *Client) SetDebug(debug bool)

SetDebug 设置调试模式

func (*Client) SetLogger

func (c *Client) SetLogger(logger *slog.Logger)

SetLogger 设置日志

type Ctx

type Ctx struct {
	// contains filtered or unexported fields
}

Ctx 请求上下文

type Header map[string]string

Header 请求头

type Opt

type Opt func(ctx *Ctx)

Opt 请求选项

func WithBody

func WithBody(body any) Opt

WithBody 设置请求体

func WithContentType

func WithContentType(contentType string) Opt

WithContentType 设置 Content-Type

func WithContext

func WithContext(c context.Context) Opt

WithContext 设置请求上下文

func WithHeader

func WithHeader(h Header) Opt

WithHeader 设置请求头

func WithHook

func WithHook(hook func(http.Header)) Opt

WithHook 设置响应 Header 钩子

func WithQuery

func WithQuery(q Query) Opt

WithQuery 设置查询参数

type Query

type Query map[string]string

Query 请求查询参数

type ReqOpt

type ReqOpt func(c *Client)

ReqOpt Client 配置选项

func WithClient

func WithClient(client *http.Client) ReqOpt

WithClient 自定义 HTTP Client

func WithDebug

func WithDebug() ReqOpt

WithDebug 开启调试模式

func WithTransport

func WithTransport(tr *http.Transport) ReqOpt

WithTransport 自定义 Transport

type Response

type Response[T any] struct {
	Code    int    `json:"code"`
	Data    T      `json:"data"`
	Message string `json:"message"`
}

Response 通用响应

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL