Fetch
Fetch 类型是用于发起 HTTP 请求的主客户端。
函数
New
创建一个新的 fetch 客户端。
go
func New(cfg ...*Config) *Fetch参数:
cfg: 可选配置
示例:
go
f := fetch.New()
f := fetch.New(&fetch.Config{
Timeout: 10 * time.Second,
})Create
创建一个带有基础 URL 的新 fetch 实例。
go
func Create(baseURL string) *Fetch参数:
baseURL: 所有请求的基础 URL
示例:
go
f := fetch.Create("https://api.example.com")方法
SetURL
设置请求 URL。
go
func (f *Fetch) SetURL(url string) *FetchSetBaseURL
设置基础 URL。
go
func (f *Fetch) SetBaseURL(url string) *FetchSetMethod
设置 HTTP 方法。
go
func (f *Fetch) SetMethod(method string) *FetchSetHeader
设置请求头。
go
func (f *Fetch) SetHeader(key, value string) *FetchSetQuery
设置查询参数。
go
func (f *Fetch) SetQuery(key, value string) *FetchSetBody
设置请求体。
go
func (f *Fetch) SetBody(body Body) *FetchSetTimeout
设置请求超时。
go
func (f *Fetch) SetTimeout(timeout time.Duration) *FetchSetBasicAuth
设置基本认证。
go
func (f *Fetch) SetBasicAuth(username, password string) *FetchSetBearerToken
设置 Bearer Token 认证。
go
func (f *Fetch) SetBearerToken(token string) *FetchSetProxy
设置代理服务器。
go
func (f *Fetch) SetProxy(proxy string) *FetchSetContext
设置用于取消的 context。
go
func (f *Fetch) SetContext(ctx context.Context) *FetchSetProgressCallback
设置上传/下载的进度回调。
go
func (f *Fetch) SetProgressCallback(callback func(percent int64, current, total int64)) *FetchGet
设置 HTTP 方法为 GET。
go
func (f *Fetch) Get(url string, config ...*Config) *FetchPost
设置 HTTP 方法为 POST。
go
func (f *Fetch) Post(url string, config ...*Config) *FetchPut
设置 HTTP 方法为 PUT。
go
func (f *Fetch) Put(url string, config ...*Config) *FetchPatch
设置 HTTP 方法为 PATCH。
go
func (f *Fetch) Patch(url string, config ...*Config) *FetchDelete
设置 HTTP 方法为 DELETE。
go
func (f *Fetch) Delete(url string, config ...*Config) *FetchHead
设置 HTTP 方法为 HEAD。
go
func (f *Fetch) Head(url string, config ...*Config) *FetchExecute
执行请求。
go
func (f *Fetch) Execute() (*Response, error)Send
Execute 的别名。
go
func (f *Fetch) Send() (*Response, error)Retry
重试请求,可选修改。
go
func (f *Fetch) Retry(before func(f *Fetch)) (*Response, error)Clone
创建 fetch 实例的克隆。
go
func (f *Fetch) Clone() *FetchConfig
返回构建的配置。
go
func (f *Fetch) Config() (*Config, error)示例
go
f := fetch.New()
f.SetBaseURL("https://api.example.com")
f.SetBearerToken("token")
f.SetTimeout(10 * time.Second)
response, err := f.Get("/users").Execute()
if err != nil {
panic(err)
}
fmt.Println(response.JSON())