# README
go-axios
简单易用的HTTP客户端,参考axios的相关实现,支持各类不同的interceptor
与transform
,特性如下:
- 支持自定义的transform,可根据应用场景指定各类不同的参数格式
- 支持Request与Response的interceptor,可针对请求参数与响应数据添加各类的转换处理
- 默认支持gzip与br两种压缩方式,节约带宽占用
- 支持启用请求中的各事件记录,可细化请求的各场景耗时
- 简单易用的Mock处理
- Error与Done事件的支持,通过监听事件可快捷收集服务的出错与性能统计
// +build ignore
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/vicanso/go-axios"
)
func main() {
// 使用默认的配置
resp, err := axios.Get("https://www.baidu.com")
if err != nil {
panic(err)
}
fmt.Println(resp.Data)
// 自定义instance,可指定client、interceptor等
ins := axios.NewInstance(&axios.InstanceConfig{
BaseURL: "https://www.baidu.com",
EnableTrace: true,
Client: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
},
// 超时设置,建议设置此字段避免无接口无超时处理
Timeout: 10 * time.Second,
// OnDone 无论成功或失败均会调用,可在此处添加统计
OnDone: func(config *axios.Config, resp *axios.Response, err error) {
fmt.Println(config)
fmt.Println(resp)
fmt.Println(err)
},
})
resp, err = ins.Get("/")
if err != nil {
panic(err)
}
buf, _ := json.Marshal(resp.Config.HTTPTrace.Stats())
fmt.Println(resp.Config.HTTPTrace.Stats())
fmt.Println(string(buf))
fmt.Println(resp.Config.HTTPTrace.Protocol)
fmt.Println(resp.Status)
fmt.Println(string(resp.Data))
}
# Functions
Delete http delete request by default instance.
Get http get request by default instance.
GetDefaultInstance get default instance.
GetInternalErrorCategory get the category of net op error.
GetStats get stats of request.
Head http head request by default instance.
MapToValues converts map[string]string to url.Values.
MapToValuesOmitEmpty converts map[string]string to url.Values, and omit empty value.
NewInstance create a new instance.
NewMultipartFile creates a multipart file writer.
Options http options request by default instance.
Patch http patch request by default instance.
Post http post request by default instance.
Put http put request by default instance.
copy from io.ReadAll ReadAll reads from r until an error or EOF and returns the data it read.
Request http request by default instance.
SetJSONMarshal set json marshal function.
SetJSONUnmarshal set json unmarshal function.
Upload upload file by default instance.
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
UserAgent client user agent.
# Variables
DefaultTransformRequest default transform request.
DefaultTransformResponse default transform response.
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Type aliases
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author