http

package
v0.0.0-...-49bc363 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2025 License: MIT Imports: 22 Imported by: 2

Documentation

Overview

Package http 提供 net/http 包的一些工具

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DownloadFile

func DownloadFile(
	ctx context.Context, targetURL string, opts ...RequestOption,
) (fileName string, content []byte, err error)

DownloadFile 下载文件

func JSON

func JSON[T any](ctx context.Context, targetURL string, opts ...RequestOption) (*T, error)

JSON 发起一个 JSON 请求

func NormalizeHeader

func NormalizeHeader(h http.Header) http.Header

NormalizeHeader 返回统一 mine key 格式的 header

func Raw

func Raw(ctx context.Context, targetURL string, opts ...RequestOption) (rsp []byte, err error)

Raw 发起一个请求, 但是返回 []byte

func ReadSSEJsonData

func ReadSSEJsonData[T any](
	ctx context.Context, r io.Reader, handler func(event T),
	opts ...RequestOption,
) error

ReadSSEReadSSEJsonDataData 处理 SSE 事件, 仅处理 'data: ' 行, 并使用 json 反序列化。

Options 目前仅支持 debug 日志

func Request

func Request(ctx context.Context, targetURL string, opts ...RequestOption) (*http.Response, error)

Request 发起一个请求, 但是返回 *http.Response。适合 HTTP SSE 场景适配

func XML

func XML[T any](ctx context.Context, targetURL string, opts ...RequestOption) (*T, error)

XML 发起一个 XML 请求

WARNING: 未测试, 请注意

func XMLGetRspBody

func XMLGetRspBody(ctx context.Context, targetURL string, opts ...RequestOption) ([]byte, error)

XMLGetRspBody 发起一个 XML 请求并返回包体字节

Types

type RequestOption

type RequestOption func(*requestOption)

发起请求的额外选项

func WithDebugger

func WithDebugger(f func(string, ...any)) RequestOption

WithDebugger 添加调试函数

func WithMethod

func WithMethod(method string) RequestOption

WithMethod 指定 HTTP 调用方法

func WithProgressCallback

func WithProgressCallback(cb func(*RequestProgress)) RequestOption

WithProgressCallback 指定请求回调, 一般用在预期 body 很大的场景, 比如下载大文件

func WithQuery

func WithQuery(q url.Values) RequestOption

WithQuery query 参数

func WithRequestBody

func WithRequestBody(req any) RequestOption

WithRequestBody 请求正文

func WithRequestCookies

func WithRequestCookies(cookies []*http.Cookie) RequestOption

WithRequestCookies 设定请求头中的 Cookie

func WithRequestHeader

func WithRequestHeader(h http.Header) RequestOption

WithRequestHeader 指定请求头参数

func WithResponseCharset

func WithResponseCharset(c encoding.Encoding) RequestOption

WithResponseCharset 指定响应 charset

func WithSSEUnmarshalErrorCallback

func WithSSEUnmarshalErrorCallback(cb func(error, string)) RequestOption

WithSSEUnmarshalErrorCallback 指定反序列化错误回调, 用在 SSE 场景。当反序列化失败时, 调用该回调, 而不返回错误 (相当于出错但 continue)

type RequestProgress

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

RequestProgress 表示请求进度, 用于回调

func (*RequestProgress) ContentLength

func (p *RequestProgress) ContentLength() int64

ContentLength 从 response header 中判断文件大小。-1 表示未知

func (*RequestProgress) ReadLength

func (p *RequestProgress) ReadLength() int64

ReadLength 表示已读取的 body 大小

func (*RequestProgress) RequestState

func (p *RequestProgress) RequestState() RequestState

RequestState 获取请求状态

type RequestState

type RequestState int

RequestState 表示请求中间阶段

const (
	// RequestInitialized 表示请求准备好, 准备发出
	RequestInitialized RequestState = iota
	// ResponseReceived 表示已经接收到响应 header, 但未读取 body
	ResponseReceived
	// ReceivingBody 表示正在读取响应 body
	ReceivingBody
)

Jump to

Keyboard shortcuts

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