http

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2020 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeJSON       = "json"
	TypeXML        = "xml"
	TypeUrlencoded = "urlencoded"
	TypeForm       = "form"
	TypeFormData   = "form-data"
	TypeHTML       = "html"
	TypeText       = "text"
	TypeMultipart  = "multipart"
)

Types we support.

Variables

View Source
var Logger = AccessLogger(consoleLogger{})
View Source
var Types = map[string]string{
	TypeJSON:       "application/json",
	TypeXML:        "application/xml",
	TypeForm:       "application/x-www-form-urlencoded",
	TypeFormData:   "application/x-www-form-urlencoded",
	TypeUrlencoded: "application/x-www-form-urlencoded",
	TypeHTML:       "text/html",
	TypeText:       "text/plain",
	TypeMultipart:  "multipart/form-data",
}

Functions

This section is empty.

Types

type Client

type Client struct {
	http.Client
	// contains filtered or unexported fields
}

func BaseURL

func BaseURL(url string) *Client

BaseURL 设置url前缀

func Cookie(k string, v string) *Client

Cookie 设置请求 Cookie

func Header(key, val string) *Client

Header 设置请求 Header

func New

func New() *Client

func Proxy

func Proxy(url string) *Client

Proxy 设置请求代理

func Query

func Query(query interface{}) *Client

Query 设置请求代理

func Retry

func Retry(n int) *Client

BaseURL 设置url前缀

func Timeout

func Timeout(timeout time.Duration) *Client

Timeout 设置请求代理

func Type

func Type(name string) *Client

Type 请求提交方式,默认json

func Use

func Use(mdl ClientMiddleware) *Client

UseRequest 增加请求中间件

func UseResponse

func UseResponse(mdl responseMiddlewareHandler) *Client

UseResponse 增加响应中间件

func UserAgent

func UserAgent(name string) *Client

UserAgent 设置请求 user-agent,默认是 chrome 75.0

func (*Client) BaseURL

func (c *Client) BaseURL(url string) *Client

BaseURL 设置url前缀

func (*Client) Clone

func (c *Client) Clone() *Client

func (*Client) Cookie

func (c *Client) Cookie(k string, v string) *Client

Cookie 设置请求 Cookie

func (*Client) Del

func (c *Client) Del(url string, args ...interface{}) (*Response, error)

Del 发起 del 请求

func (*Client) DoRequest

func (c *Client) DoRequest(method, u string, data ...interface{}) (resp *Response, err error)

func (*Client) Get

func (c *Client) Get(url string, args ...interface{}) (*Response, error)

Get 发起 get 请求

func (*Client) Head

func (c *Client) Head(url string, data ...interface{}) (*Response, error)

Head 发起 head 请求

func (*Client) Header

func (c *Client) Header(k string, v interface{}) *Client

Header 设置请求 Header

func (*Client) Options

func (c *Client) Options(url string, args ...interface{}) (*Response, error)

Options 发起 get 请求

func (*Client) Patch

func (c *Client) Patch(url string, args ...interface{}) (*Response, error)

Patch 发起 patch 请求

func (*Client) Post

func (c *Client) Post(url string, args ...interface{}) (*Response, error)

Post 发起 post 请求

func (*Client) Proxy

func (c *Client) Proxy(url string) *Client

Proxy 设置请求代理

func (*Client) Put

func (c *Client) Put(url string, args ...interface{}) (*Response, error)

Put 发起 put 请求

func (*Client) Query

func (c *Client) Query(query interface{}) *Client

Query 增加查询参数

func (*Client) Retry

func (c *Client) Retry(n int) *Client

BaseURL 设置url前缀

func (*Client) Safe

func (c *Client) Safe(s ...bool) *Client

func (*Client) Timeout

func (c *Client) Timeout(timeout time.Duration) *Client

Timeout 请求超时时间

func (*Client) TransformRequest

func (c *Client) TransformRequest(h requestMiddlewareHandler) *Client

TransformRequest 增加请求中间件

func (*Client) TransformResponse

func (c *Client) TransformResponse(h responseMiddlewareHandler) *Client

TransformResponse 增加响应中间件

func (*Client) Type

func (c *Client) Type(ty string) *Client

Type 请求提交方式,默认json

func (*Client) Use

func (c *Client) Use(mdl ClientMiddleware) *Client

func (*Client) UserAgent

func (c *Client) UserAgent(name string) *Client

UserAgent 设置请求 user-agent,默认是 chrome 75.0

type ClientMiddleware

type ClientMiddleware interface {
	TransformRequest(*Client, *http.Request) *Client
	TransformResponse(*Client, *http.Request, *Response) *Response
}

type HttpLogger

type HttpLogger struct {
	Logger           printLogger
	MaxLoggerBodyLen int64
}

打印 HTTP 请求响应日志

func AccessLogger

func AccessLogger(logger printLogger, bodyLimit ...int64) *HttpLogger

func (*HttpLogger) TransformRequest

func (l *HttpLogger) TransformRequest(c *Client, req *http.Request) *Client

打印 HTTP 请求日志

func (*HttpLogger) TransformResponse

func (l *HttpLogger) TransformResponse(c *Client, req *http.Request, resp *Response) *Response

打印 HTTP 响应日志 warning: 会先读取一遍 Response.Body,如果该中间件导致了 http 下载异常问题,请关闭该中间件

type Response

type Response struct {
	Client   *Client
	Request  *http.Request
	Response *http.Response

	Err    *ResponseError
	IsRead bool
	// contains filtered or unexported fields
}

func Del

func Del(url string, data ...interface{}) (*Response, error)

Del 发起 delete 请求,body 是请求带的参数,可使用json字符串或者结构体

func Get

func Get(url string, data ...interface{}) (*Response, error)

Get 发起 get 请求, query 查询参数

func Head(url string, data ...interface{}) (*Response, error)

Head 发起 head 请求

func Options

func Options(url string, data ...interface{}) (*Response, error)

Options 发起 options 请求,query 查询参数

func Patch

func Patch(url string, data ...interface{}) (*Response, error)

Patch 发起 patch 请求,body 是请求带的参数,可使用json字符串或者结构体

func Post

func Post(url string, data ...interface{}) (*Response, error)

Post 发起 post 请求,body 是请求带的参数,可使用json字符串或者结构体

func Put

func Put(url string, data ...interface{}) (*Response, error)

Put 发起 put 请求,body 是请求带的参数,可使用json字符串或者结构体

func (*Response) AddError

func (rp *Response) AddError(err error)

AddError 手动增加错误

func (*Response) Body

func (rp *Response) Body() (bt []byte)

Body 获取响应的原始数据

func (*Response) Error

func (rp *Response) Error() string

Error 实现 error interface

func (*Response) GetError

func (rp *Response) GetError() error

GetError 获取错误

func (*Response) JSON

func (rp *Response) JSON(v interface{}) error

JSON 使用 JSON 解析响应 Body 数据

func (*Response) ReadAllBody

func (rp *Response) ReadAllBody() (bt []byte, err error)

ReadAllBody 读取响应的 Body 流

func (*Response) SetBody added in v1.4.1

func (rp *Response) SetBody(bt []byte)

SetBody 重置响应的 Body

func (*Response) Status

func (rp *Response) Status() int

Status StatusCode 别名

func (*Response) StatusCode

func (rp *Response) StatusCode() int

StatusCode 获取 HTTP 响应状态码

func (*Response) String

func (rp *Response) String() string

String 将响应的 Body 数据转成字符串

type ResponseError

type ResponseError struct {
	// contains filtered or unexported fields
}

func (*ResponseError) Add

func (e *ResponseError) Add(err error)

func (*ResponseError) Error

func (e *ResponseError) Error() string

Jump to

Keyboard shortcuts

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