golang有用的库及工具 之 fasthttp客户端 最通用最有效最简单使用方式
来源:互联网 发布:java socket长连接心跳 编辑:程序博客网 时间:2024/05/25 01:36
fasthttp 是go语言中最常用性能最好的http请求库。
那么如何使用fasthttp 客户端调用http请求。
常用http kv请求:
//http请求func doTimeout(arg *fasthttp.Args, method string, requestURI string, cookies map[string]interface{}) ([]byte, int, error) { req := &fasthttp.Request{} switch method { case "GET": req.Header.SetMethod(method) // 拼接url requestURI = requestURI + "?" + arg.String() case "POST": req.Header.SetMethod(method) arg.WriteTo(req.BodyWriter()) } if cookies != nil { for key, v := range cookies { req.Header.SetCookie(key, v.(string)) } } req.SetRequestURI(requestURI) resp := &fasthttp.Response{} err := gCli.DoTimeout(req, resp, time.Second*30) return resp.Body(), resp.StatusCode(), err}
json body 使用:
func doJsonTimeout(method string, url, bodyjson string) ([]byte, int, error) { req := &fasthttp.Request{} resp := &fasthttp.Response{} switch method { case "GET": req.Header.SetMethod(method) case "POST": req.Header.SetMethod(method) } req.Header.SetContentType("application/json") req.SetBodyString(bodyjson) req.SetRequestURI(url) err := gCli.DoTimeout(req, resp, time.Second*30) return resp.Body(), resp.StatusCode(), err}
test:
func main() { var arg = &fasthttp.Args{} arg.Set("hello","world") fmt.Println(doTimeout(arg, "GET","http://127.0.0.1/hello",nil)) fmt.Println(doJsonTimeout("GET","http://127.0.0.1/hello","{\"hello\":\"world\"}"))}
阅读全文
0 0
- golang有用的库及工具 之 fasthttp客户端 最通用最有效最简单使用方式
- 最简单最有用的英文口语
- 最简单的 Golang web
- 最简单的Redis客户端实现——Golang
- MyEclipse2014a 最简单最有效的汉化方法---eclipse和其他版本通用
- MyEclipse2014a 最简单最有效的汉化方法---eclipse和其他版本通用
- 最简单的通用Makefile
- 最简单的Druid使用方式
- 分析最常见最有效的公共传播方方式
- 异常最简单使用方式
- 网页设计多伦多 - 最有效的工具
- 对付黑屏最简单最有效的方法
- asp导出excel最简单最有效的方法
- 最简单的系统日志收集方式 elk + rsyslog客户端
- 最简单有效抵御垃圾邮件的方法
- 五脏排毒最简单有效的方法
- 五脏排毒最简单有效的方法
- 五脏排毒最简单有效的方法
- JZOJ 5439. 【NOIP2017提高A组集训10.31】Calculate
- PowerDesigner设置表字符编码 utf-8
- C语言易错知识点整理
- nginx 相对 apache 比较,总结一下
- 机器学习 (一)
- golang有用的库及工具 之 fasthttp客户端 最通用最有效最简单使用方式
- 【实战】模仿设置中心
- Toast之悬浮窗(不需要权限)
- 数据数据
- vue项目构建
- 诡异的Linux磁盘空间被占用问题,根目录满了,df和du占用不一样【转】
- python中yield的使用
- Python转换GMT时间为时间戳,并计算时间差
- IDEA避坑整理