httpRequest

package
v1.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2019 License: MIT Imports: 10 Imported by: 0

README

go http client

http request client library,base on go-resty.
go-resty: https://github.com/go-resty/resty

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultReqTimeout time.Duration = 5 * time.Second

默认请求超时

Functions

func NewClient added in v1.3.4

func NewClient() *resty.Client

NewClient创建一个resty客户端,支持post,get,delete,head,put,patch,file文件上传等 可以快速使用go-resty/resty上面的方法 参考文档: https://github.com/go-resty/resty

Types

type ApiStdRes

type ApiStdRes struct {
	Code    int
	Message string
	Data    interface{}
}

ApiStdRes 标准的api返回格式

type Reply added in v1.3.3

type Reply struct {
	Err  error  //请求过程中,发生的error
	Body []byte //返回的body内容
}

Reply 请求后的结果

func (*Reply) Json added in v1.3.3

func (r *Reply) Json(data interface{}) error

Json 将响应的结果Reply解析到data 对返回的Reply.Body做json反序列化处理

func (*Reply) Text added in v1.3.3

func (r *Reply) Text() string

Text 返回Reply.Body文本格式

type ReqOpt added in v1.3.3

type ReqOpt struct {
	Params  map[string]interface{} //get,delete的Params参数
	Data    map[string]interface{} //post请求form data表单数据
	Headers map[string]interface{} //header头信息

	//cookie参数设置
	Cookies        map[string]interface{} //cookie信息
	CookiePath     string                 //可选参数
	CookieDomain   string                 //cookie domain可选
	CookieMaxAge   int                    //cookie MaxAge
	CookieHttpOnly bool                   //cookie httpOnly

	//支持post,put,patch以json格式传递,[]int{1, 2, 3},map[string]string{"a":"b"}格式
	//json支持[],{}数据格式,主要是golang的基本数据类型,就可以
	//直接调用SetBody方法,自动添加header头"Content-Type":"application/json"
	Json interface{}

	//支持文件上传的参数
	FileName      string //文件名称
	FileParamName string //文件上传的表单file参数名称
}

ReqOpt 请求参数设置

func (ReqOpt) ParseData added in v1.3.3

func (ReqOpt) ParseData(d map[string]interface{}) map[string]string

ParseData 解析ReqOpt Params和Data

type Service added in v1.3.3

type Service struct {
	BaseUri         string        //请求地址url的前缀
	Timeout         time.Duration //请求超时限制
	Proxy           string        //请求设置的http_proxy代理
	EnableKeepAlive bool          //是否允许长连接方式请求接口,默认短连接方式
}

Service 请求句柄设置

func (*Service) Do added in v1.3.3

func (s *Service) Do(method string, reqUrl string, opt *ReqOpt) *Reply

Do 请求方法 method string 请求的方法get,post,put,patch,delete,head等 uri string 请求的相对地址,如果BaseUri为空,就必须是完整的url地址 opt *ReqOpt 请求参数ReqOpt

func (*Service) GetResult added in v1.3.4

func (s *Service) GetResult(resp *resty.Response, err error) *Reply

GetData 处理请求的结果

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL