Gout
gout to become the Swiss Army Knife of the http client @^^@---> gout 是http client领域的瑞士军刀,小巧,强大,犀利。具体用法可看文档,如使用迷惑或者API用得不爽都可提issues
Install / Use
/learn @guonaihong/GoutREADME
gout
gout 是go写的http 客户端,为提高工作效率而开发
构架

feature
- 支持设置 GET/PUT/DELETE/PATH/HEAD/OPTIONS
- 支持设置请求 http header(可传 struct,map,array,slice 等类型)
- 支持设置 URL query(可传 struct,map,array,slice,string 等类型)
- 支持设置 json 编码到请求 body 里面(可传struct, map, string, []byte 等类型)
- 支持设置 xml 编码到请求 body 里面(可传struct, string, []byte 等类型)
- 支持设置 yaml 编码到请求 body 里面(可传struct, map, string, []byte 等类型)
- 支持设置 protobuf 编码到请求 body里面(可传struct)
- 支持设置 form-data(可传 struct, map, array, slice 等类型)
- 支持设置 x-www-form-urlencoded(可传 struct,map,array,slice 等类型)
- 支持设置 io.Reader,uint/uint8/uint16...int/int8...string...[]byte...float32,float64 至请求 body 里面
- 支持解析响应body里面的json,xml,yaml至结构体里(BindJSON/BindXML/BindYAML)
- 支持解析响应body的内容至io.Writer, uint/uint8...int/int8...string...[]byte...float32,float64
- 支持解析响应header至结构体里
- 支持接口性能benchmark,可控制压测一定次数还是时间,可控制压测频率
- 支持retry-backoff,可以指定重试条件
- 支持发送裸http数据包
- 支持导出curl命令
- 传入自定义*http.Client
- 支持请求中间件(https://github.com/antlabs/gout-middleware)
- 支持响应中间件ResponseUse
- 支持设置chunked数据格式发送
- 支持body, header的数据校验
- 支持通过build tag自由选择不同的json序列化方式(可选jsoniter,go_json,sonic等)
- 等等更多
演示
<details>
内容
-
- GET POST PUT DELETE PATH HEAD OPTIONS
- Query Parameters
- http header *Do not convert http headers
- http body
- proxy
- cookie
- context
- unix socket
- http2 doc
- debug mode
Installation
go get github.com/guonaihong/gout
example
examples 目录下面的例子,都是可以直接跑的。如果觉得运行例子还是不明白用法,可以把你迷惑的地方写出来,然后提issue
运行命令如下
cd _example
# GOPROXY 是打开go module代理,可以更快下载模块
# 第一次运行需要加GOPROXY下载模块,模块已安装的直接 go run 01-color-json.go 即可
env GOPROXY=https://goproxy.cn go run 01-color-json.go
build tag
Gout默认使用语言内置的encoding/json包。但是如果你想使用其他的json包,可以通过build tag来修改。
go build -tags=jsoniter .
go build -tags=go_json .
go build -tags="sonic avx" .
quick start
package main
import (
"fmt"
"github.com/guonaihong/gout"
"time"
)
// 用于解析 服务端 返回的http body
type RspBody struct {
ErrMsg string `json:"errmsg"`
ErrCode int `json:"errcode"`
Data string `json:"data"`
}
// 用于解析 服务端 返回的http header
type RspHeader struct {
Sid string `header:"sid"`
Time int `header:"time"`
}
func main() {
rsp := RspBody{}
header := RspHeader{}
//code := 0
err := gout.
// POST请求
POST("127.0.0.1:8080").
// 打开debug模式
Debug(true).
// 设置查询字符串
SetQuery(gout.H{"page": 10, "size": 10}).
// 设置http header
SetHeader(gout.H{"X-IP": "127.0.0.1", "sid": fmt.Sprintf("%x", time.Now().UnixNano())}).
// SetJSON设置http body为json
// 同类函数有SetBody, SetYAML, SetXML, SetForm, SetWWWForm
SetJSON(gout.H{"text": "gout"}).
// BindJSON解析返回的body内容
// 同类函数有BindBody, BindYAML, BindXML
BindJSON(&rsp).
// 解析返回的http header
BindHeader(&header).
// http code
// Code(&code).
// 结束函数
Do()
// 判断错误
if err != nil {
fmt.Printf("send fail:%s\n", err)
}
}
/*
> POST /?page=10&size=10 HTTP/1.1
> Sid: 15d9b742ef32c130
> X-Ip: 127.0.0.1
> Content-Type: application/json
>
{
"text": "gout"
}
*/
API examples
GET POST PUT DELETE PATH HEAD OPTIONS
package main
import (
"github.com/guonaihong/gout"
)
func main() {
url := "https://github.com"
// 发送GET方法
gout.GET(url).Do()
// 发送POST方法
gout.POST(url).Do()
// 发送PUT方法
gout.PUT(url).Do()
// 发送DELETE方法
gout.DELETE(url).Do()
// 发送PATH方法
gout.PATCH(url).Do()
// 发送HEAD方法
gout.HEAD(url).Do()
// 发送OPTIONS
gout.OPTIONS(url).Do()
}
GET POST PUT DELETE PATH HEAD OPTIONS template
package main
import (
"github.com/guonaihong/gout"
)
type testURLTemplateCase struct {
Host string
}
func main() {
url := "https://{{.Host}}"
// 发送GET方法
gout.GET(url, testURLTemplateCase{Host:"www.qq.com"}).Do()
// 发送POST方法
gout.POST(url, testURLTemplateCase{Host:"www.github.com"}).Do()
// 发送PUT方法
gout.PUT(url, testURLTemplateCase{Host:"www.baidu.com"}).Do()
// 发送DELETE方法
gout.DELETE(url, testURLTemplateCase{Host:"www.google.com"}).Do()
// 发送PATH方法
gout.PATCH(url, testURLTemplateCase{Host:"www.google.com"}).Do()
// 发送HEAD方法
gout.HEAD(url, testURLTemplateCase{Host:"www.google.com"}).Do()
// 发送OPTIONS
gout.OPTIONS(url, testURLTemplateCase{Host:"www.google.com"}).Do()
}
Query Parameters
SetQuery
package main
import (
"fmt"
"github.com/guonaihong/gout"
"time"
)
func main() {
err := gout.
//设置GET请求和url,:8080/test.query是127.0.0.1:8080/test.query的简写
GET(":8080/test.query").
//打开debug模式
Debug(true).
//设置查询字符串
SetQuery(gout.H{
"q1": "v1",
"q2": 2,
"q3": float32(3.14),
"q4": 4.56,
"q5": time.Now().Unix(),
"q6": time.Now().UnixNano(),
"q7": time.Now().Format("2006-01-02")}).
//结束函数
Do()
if err != nil {
fmt.Printf("%s\n", err)
return
}
}
/*
> GET /test.query?q1=v1&q2=2&q3=3.14&q4=4.56&q5=1574081600&q6=1574081600258009213&q7=2019-11-18 HTTP/1.1
>
< HTTP/1.1 200 OK
< Content-Length: 0
*/
SetQuery支持的更多数据类型
package main
import (
"github.com/guonaihong/gout"
)
func main() {
code := 0
err := gout.
//发送GET请求 :8080/testquery是127.0.0.1:8080/testquery简写
GET(":8080/testquery").
// 设置查询字符串
SetQuery( /*看下面支持的情况*/ ).
//解析http code,如不关心服务端返回状态吗,不设置该函数即可
Code(&code).
Do()
if err != nil {
}
}
/*
SetQuery支持的类型有
* string
* map[string]interface{},可以使用gout.H别名
* struct
* array, slice(长度必须是偶数)
*/
// 1.string
SetQuery("check_in=2019-06-18&check_out=2018-06-18")
// 2.gout.H 或者 map[string]interface{}
SetQuery(gout.H{
"check_in":"2019-06-18",
"check_out":"2019-0
Related Skills
node-connect
335.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
xurl
335.8kA CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.
frontend-design
82.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
335.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
