Documentation
¶
Overview ¶
Package http 提供 net/http 包的一些工具
Index ¶
- func DownloadFile(ctx context.Context, targetURL string, opts ...RequestOption) (fileName string, content []byte, err error)
- func JSON[T any](ctx context.Context, targetURL string, opts ...RequestOption) (*T, error)
- func NormalizeHeader(h http.Header) http.Header
- func Raw(ctx context.Context, targetURL string, opts ...RequestOption) (rsp []byte, err error)
- func ReadSSEJsonData[T any](ctx context.Context, r io.Reader, handler func(event T), opts ...RequestOption) error
- func Request(ctx context.Context, targetURL string, opts ...RequestOption) (*http.Response, error)
- func XML[T any](ctx context.Context, targetURL string, opts ...RequestOption) (*T, error)
- func XMLGetRspBody(ctx context.Context, targetURL string, opts ...RequestOption) ([]byte, error)
- type RequestOption
- func WithDebugger(f func(string, ...any)) RequestOption
- func WithMethod(method string) RequestOption
- func WithProgressCallback(cb func(*RequestProgress)) RequestOption
- func WithQuery(q url.Values) RequestOption
- func WithRequestBody(req any) RequestOption
- func WithRequestCookies(cookies []*http.Cookie) RequestOption
- func WithRequestHeader(h http.Header) RequestOption
- func WithResponseCharset(c encoding.Encoding) RequestOption
- func WithSSEUnmarshalErrorCallback(cb func(error, string)) RequestOption
- type RequestProgress
- type RequestState
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 NormalizeHeader ¶
NormalizeHeader 返回统一 mine key 格式的 header
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 XMLGetRspBody ¶
XMLGetRspBody 发起一个 XML 请求并返回包体字节
Types ¶
type RequestOption ¶
type RequestOption func(*requestOption)
发起请求的额外选项
func WithProgressCallback ¶
func WithProgressCallback(cb func(*RequestProgress)) RequestOption
WithProgressCallback 指定请求回调, 一般用在预期 body 很大的场景, 比如下载大文件
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 )
Click to show internal directories.
Click to hide internal directories.