go http请求库HttpRequest

原文链接

目录

安装

go get https://github.com/kirinlabs/HttpRequest

发送请求

导入HttpRequest

import "github.com/kirinlabs/HttpRequest"

实例化

req := HttpRequest.NewRequest()

Get请求

然后,尝试获取某个网页。我们来获取 Github 的公共时间线

返回一个res的Response对象和err的Error对象

res,err := req.Get("https://api.github.com/events")

Post 请求

//无参请求res,err := req.Post("https://www.baidu.com") //请求体为文本res,err := req.Post("https://www.baidu.com","hello") //请求体为Json字符串res,err := req.Post("https://www.baidu.com","{\"name\":\"github\"}") //map传参res.err := req.Post("https://www.baidu.com",map[string]interface{}{ "name":"github", "type":1,})

 也可以不用实例化,直接发送请求

快速发送Get请求

res,err := HttpRequest.Get("https://www.baidu.com")res,err := HttpRequest.Get("https://www.baidu.com","title=baidu")

快速发送Post请求

res,err := HttpRequest.Post("https://www.baidu.com")res,err := HttpRequest.Post("https://www.baidu.com","title=baidu&type=pdf")res,err := HttpRequest.Post("https://www.baidu.com",map[string]interface{}{"title":"baidu",})

快速发送JSON请求

res,err := HttpRequest.JSON().Post("https://www.baidu.com",map[string]interface{}{ "title":"baidu",}) res,err := HttpRequest.JSON().Post("https://www.baidu.com",`{"title":"baidu","type":"pdf"}`)

传递URL参数

你想为URL的查询字符串(query string)传递数据。如:手工构建URL,http://www.baidu.com/index?key=value。HttpRequest允许你使用第2个参数以字符串"id=100&name=github"或map[string]interface{}{"id":10,"name":"github"}字典的形式把数据传递给URL:

手工传参

res,err := req.Get("https://www.baidu.com/index?name=github")

字符串传参

res,err := req.Get("https://www.baidu.com/index?name=github","id=100&type=1")

map传参

res,err := req.Get("https://www.baidu.com/index?name=github",map[string]interface{}{ "id":10, "type":1,})

响应内容

能读取服务器响应的内容

res,err := req.Post("https://api.github.com/events")

 获取服务器返回的内容

body,err := res.Body()fmt.Println(string(body))

获取服务器响应状态码

res.StatusCode()

获取服务器响应Headers

res.Headers()

返回一个map[string]string的字典

获取请求响应时间

res.Time()

Json响应内容

HttpRequest内置JSON解码,来解析JSON数据

//Format the json return valuebody, err := res.Json() fmt.Println(body)

如果JSON解码失败,会返回一个err错误

定制请求头

如果想为请求添加HTTP头部信息,只需要简单的传一个map给SetHeaders方法

req.SetHeaders(map[string]string{ "Content-Type":"application/json", "Source":"api",})

注:所有header值必须是字符串,SetHeaders可以多次调用,如果Key重复则会覆盖前面设置的值

BasicAuth 认证

如果想为请求添加HTTP头部信息,只需要简单的传一个map给SetHeaders方法

req.SetBasicAuth("username","password")

 

JSON请求

如果想以json方式发送请求,HttpRequest支持2种方式

设置Header头部信息

req.SetHeaders(map[string]string{"Content-Type":"application/json"}) req.Post("https://www.baidu.com","{\"name\":\"github\"}")

调用req.JSON()内置方法

//直接发磅Json字符串参数res,err := req.JSON().Post("https://www.baidu.com","{\"name\":\"github\"}") //自动将Map以Json方式发送参数res,err := req.JSON().Post("https://www.baidu.com",map[string]interface{}{ "name":"github"})

Cookie

req.SetCookies(map[string]string{ "name":"jason"})

超时

req.SetTimeout(5)

关闭证书验证

当请求https协议时提示x509: certificate signed by unknown authority时,可关闭证书验证

req.SetTLSClient(&tls.Config{InsecureSkipVerify: true})

调试模式

req.Debug(true)

连接操作

而且还支持连接操作

req := HttpRequest.NewRequest().Debug(true).SetTimeout(5).SetHeader()

Respone对象

获取返回的Response对象resp.Response()获取返回码resp.StatusCode()获取Body主体信息 resp.Body() 获取请求耗时resp.Time() string 单位是毫秒获取真实Urlres.Url()

  

 

相关文章