modulepackage
1.0.5
Repository: https://github.com/topology-zero/httpclient.git
Documentation: pkg.go.dev
# README
httpclient
对 golang http 库进行封装,更优雅的调用网络接口
最新版本
go get github.com/topology-zero/[email protected]
示例使用
GET 请求
query := map[string]string{
"a": "1",
"b": "2",
}
request := httpclient.NewHttpRequest(httpclient.GET, "https://foo.com", httpclient.WithQuery(query))
resp, err := request.DoHttpRequest()
if err != nil {
panic(err)
}
log.Println(string(resp))
POST JSON
请求
type reqStruct struct {
id int
name string
}
req := reqStruct{
id: 10086,
name: "中国移不动",
}
request := httpclient.NewHttpRequest(httpclient.POST, "https://foo.com", httpclient.WithJson(req))
resp, err := request.DoHttpRequest()
if err != nil {
panic(err)
}
log.Println(string(resp))
POST FROM-DATA
请求 -- 附带文件
req := httpclient.UploadFile{
Field: "file",
FileName: "image.png",
File: bytes.NewReader([]byte{}),
}
request := httpclient.NewHttpRequest(httpclient.POST, "https://foo.com", httpclient.WithMultipartFrom(&req, nil))
resp, err := request.DoHttpRequest()
if err != nil {
panic(err)
}
log.Println(string(resp))
POST FROM-DATA
请求 -- 不附带文件
req := map[string]string{
"a": "1",
"b": "2",
}
request := httpclient.NewHttpRequest(httpclient.POST, "https://foo.com", httpclient.WithMultipartFrom(nil, req))
resp, err := request.DoHttpRequest()
if err != nil {
panic(err)
}
log.Println(string(resp))
POST FROM-DATA
请求 -- 附带文件 + 其他字段
file := httpclient.UploadFile{
Field: "file",
FileName: "image.png",
File: bytes.NewReader([]byte{}),
}
data := map[string]string{
"a": "1",
"b": "2",
}
request := httpclient.NewHttpRequest(httpclient.POST, "https://foo.com", httpclient.WithMultipartFrom(&file, data))
resp, err := request.DoHttpRequest()
if err != nil {
panic(err)
}
POST x-www-form-urlencoded
请求
req := map[string]any{
"a": 1,
"b": "2",
}
request := httpclient.NewHttpRequest(httpclient.POST, "https://foo.com", httpclient.WithFromData(req))
resp, err := request.DoHttpRequest()
if err != nil {
panic(err)
}
log.Println(string(resp))
PUT 请求
type reqStruct struct {
name string
}
req := reqStruct{
name: "中国移不动",
}
request := httpclient.NewHttpRequest(httpclient.PUT, "https://foo.com/10086", httpclient.WithJson(req))
resp, err := request.DoHttpRequest()
if err != nil {
panic(err)
}
log.Println(string(resp))
DELETE 请求
request := httpclient.NewHttpRequest(httpclient.DELETE, "https://foo.com/10086")
resp, err := request.DoHttpRequest()
if err != nil {
panic(err)
}
log.Println(string(resp))
自定义错误日志处理
request := httpclient.NewHttpRequest(httpclient.DELETE, "https://foo.com/10086", httpclient.WithLog(logrus.StandardLogger()))
resp, err := request.DoHttpRequest()
if err != nil {
panic(err)
}
log.Println(string(resp))
# Functions
No description provided by the author
No description provided by the author
No description provided by the author
NewHttpRequest 构建一个 Request 对象.
WithClient 设置 http 客户端.
WithFromData 设置请求结构为 from-urlencoded.
WithHeader 添加请求头.
WithJson 设置请求结构为 JSON.
WithLog 设置日志输出.
WithMultipartFrom 设置请求结构为 form-data.
No description provided by the author
WithQuery 设置 GET 请求参数.
WithRawBody 设置自定义请求结构.
WithRetry 设置重试次数 默认不重试.
WithTimeout 设置请求超时.
# Variables
No description provided by the author
# Interfaces
No description provided by the author
# Type aliases
No description provided by the author