1 Star 0 Fork 0

trako/gentleman

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
plugin.go 815 Bytes
一键复制 编辑 原始数据 按行查看 历史
trako 提交于 2024-03-14 16:53 +08:00 . fix包错误导入
package main
import (
"fmt"
"net/url"
"gitee.com/trako/gentleman/context"
"gitee.com/trako/gentleman/plugin"
"gitee.com/trako/gentleman/plugins/headers"
)
func main() {
// Create a new client
cli := gentleman.New()
// Define a custom header
cli.Use(headers.Set("Token", "s3cr3t"))
// Create a request plugin to define the URL
cli.Use(plugin.NewRequestPlugin(func(ctx *context.Context, h context.Handler) {
u, _ := url.Parse("http://httpbin.org/headers")
ctx.Request.URL = u
h.Next(ctx)
}))
// Perform the request
res, err := cli.Request().Send()
if err != nil {
fmt.Printf("Request error: %s\n", err)
return
}
if !res.Ok {
fmt.Printf("Invalid server response: %d\n", res.StatusCode)
return
}
fmt.Printf("Status: %d\n", res.StatusCode)
fmt.Printf("Body: %s", res.String())
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/trako/gentleman.git
git@gitee.com:trako/gentleman.git
trako
gentleman
gentleman
v2.1.1

搜索帮助