1 Star 0 Fork 0

trako/gentleman

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
context.go 923 Bytes
一键复制 编辑 原始数据 按行查看 历史
Tomas Aparicio 提交于 2017-03-18 15:48 . feat(v2): push v2 preview release
package main
import (
"fmt"
"gopkg.in/h2non/gentleman.v2"
)
func main() {
// Create new request instance
req := gentleman.NewRequest()
req.Method("GET")
// Define target URL
req.URL("http://httpbin.org/headers")
// Set a new header field
req.SetHeader("Client", "gentleman")
// Set sample context data
req.Context.Set("foo", "bar")
req.Context.Set("bar", "baz")
// Set sample body as string
req.BodyString("hello, gentleman!")
// Output all context data
fmt.Println(req.Context.GetAll())
// Perform the request
res, err := req.Do()
if err != nil {
fmt.Printf("Request error: %s\n", err)
return
}
if !res.Ok {
fmt.Printf("Invalid server response: %d\n", res.StatusCode)
return
}
// Set sample context data
fmt.Println(req.Context.GetString("foo"))
fmt.Println(req.Context.GetString("bar"))
// Reads the whole body and returns it as string
fmt.Printf("Body: %s", res.String())
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/trako/gentleman.git
git@gitee.com:trako/gentleman.git
trako
gentleman
gentleman
v2.0.2

搜索帮助