main.go
package main
import (
"fmt"
"net/http"
"github.com/zituocn/esme"
)
func main() {
// 请求一个天气预报的接口
ctx := esme.HttpPost("https://tenapi.cn/wether/?city=%E6%88%90%E9%83%BD")
// 成功的回调
ctx.SetSucceedFunc(func(c *esme.Context) {
fmt.Println("请求成功了:")
fmt.Println("返回值 :", c.ToString())
})
// 失败的回调
ctx.SetFailedFunc(func(c *esme.Context) {
fmt.Println("请求出错了...")
fmt.Println("返回状态值 :", c.Response.StatusCode)
fmt.Println("返回值 :", c.ToString())
})
// 执行请求
ctx.Do()
}
header := &http.Header{}
header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36")
header.Set("Content-Type", "application/json")
ctx := esme.HttpPost("https://tenapi.cn/wether/?city=%E6%88%90%E9%83%BD", header)
body := `{"username":"testname","password":"1234567890"}`
payLoad := []byte(body)
ctx := esme.HttpPost("https://tenapi.cn/wether/?city=%E6%88%90%E9%83%BD", payLoad)
formData := make(esme.FormData)
formData["username"] = "testname"
formData["password"] = "1234567890"
ctx := esme.HttpPost("https://tenapi.cn/wether/?city=%E6%88%90%E9%83%BD", formData)
一个cookie值
cookie := &http.Cookie{
Name: "name",
Value: "value",
}
ctx := esme.HttpPost("https://tenapi.cn/wether/?city=%E6%88%90%E9%83%BD", cookie)
多个cookie值
cookie := make([]*esme.Cookie, 0)
cookie = append(cookie, &esme.Cookie{
Name: "name",
Value: "value",
})
ctx := esme.HttpPost("https://tenapi.cn/wether/?city=%E6%88%90%E9%83%BD", cookie)
ctx.SetProxy("http://10.10.10.10:8888")
请求开始的回调
func (c *Context) SetStartFunc(fn CallbackFunc) *Context
请求完成的回调
func (c *Context) SetCompleteFunc(fn CallbackFunc) *Context
请求成功的回调
func (c *Context) SetSucceedFunc(fn CallbackFunc) *Context
请求失败的回调
func (c *Context) SetFailedFunc(fn CallbackFunc) *Context
需要重试的回调
func (c *Context) SetRetryFunc(fn CallbackFunc) *Context
func (c *Context) SetTransport(f func() *http.Transport) *Context
演示代码
ctx := HttpPost("https://tenapi.cn/wether/?city=%E6%88%90%E9%83%BD")
ctx.SetTransport(func() *http.Transport {
return &http.Transport{
MaxIdleConns: 100,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
})
TODO: