httpclient

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2019 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package httpclient http客户端

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DNSResolverFunc added in v1.0.8

type DNSResolverFunc func(host string) (ip string, err error)

DNSResolver DNS解析

type DialContext added in v1.0.7

type DialContext func(ctx context.Context, network, addr string) (net.Conn, error)

type Option added in v1.0.6

type Option func(*options)

func WithClient added in v1.0.6

func WithClient(client *http.Client) Option

WithClient 自定义http client

func WithConnectTimeout added in v1.0.9

func WithConnectTimeout(timeout time.Duration) Option

WithConnectTimeout 设置连接超时

func WithCookieJar added in v1.1.0

func WithCookieJar(cookieJar http.CookieJar) Option

func WithDNSResolver added in v1.0.7

func WithDNSResolver(dnsResolver DNSResolverFunc) Option

func WithDebug added in v1.0.6

func WithDebug() Option

WithDebug 开启调试模式

func WithDisableKeepAlive added in v1.0.6

func WithDisableKeepAlive() Option

WithDisableKeepAlive 连接重用

func WithEnableDefaultHeader added in v1.0.6

func WithEnableDefaultHeader() Option

WithEnableDefaultHeader 设置默认header

func WithMaxIdleConnsPerHost added in v1.0.9

func WithMaxIdleConnsPerHost(n int) Option

WithMaxIdleConnsPerHost 设置每个host最大空闲连接数

func WithProxyURL added in v1.0.6

func WithProxyURL(proxyURL string) Option

WithProxyURL 设置代理

func WithRetryTime added in v1.0.6

func WithRetryTime(retryTimes int) Option

WithRetryTime 设置重试次数

func WithShouldRetryFunc added in v1.0.6

func WithShouldRetryFunc(f func(*http.Response, error) bool) Option

WithShouldRetryFunc 自定义是否需要重试

func WithTimeout added in v1.0.6

func WithTimeout(timeout time.Duration) Option

WithTimeout 设置超时

type Request

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

Request http请求

func NewRequest

func NewRequest(opt ...Option) *Request

NewRequest 创建request

func (*Request) Get

func (req *Request) Get(url string, data url.Values, header http.Header) (*Response, error)

Get get请求

func (*Request) Post

func (req *Request) Post(url string, data interface{}, header http.Header) (*Response, error)

Post 普通post请求

func (*Request) PostJSON

func (req *Request) PostJSON(url string, data interface{}, header http.Header) (*Response, error)

PostJSON 发送json body

type Response

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

Response http响应

func Get

func Get(url string, data url.Values, header http.Header) (*Response, error)

Get get请求

func Post

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

Post 普通post请求

func PostJSON

func PostJSON(url string, data interface{}, header http.Header) (*Response, error)

PostJSON 发送json body

func (*Response) Bytes

func (resp *Response) Bytes() ([]byte, error)

Bytes 读取http.Body, 返回bytes

func (*Response) DecodeJSON

func (resp *Response) DecodeJSON(v interface{}) error

DecodeJSON http.Body json decode

func (*Response) Discard

func (resp *Response) Discard() (int64, error)

Discard 丢弃http.body

func (*Response) Header

func (resp *Response) Header() http.Header

Header 获取header

func (*Response) IsStatusOK

func (resp *Response) IsStatusOK() bool

IsStatusOK 响应码是否为200

func (*Response) Raw

func (resp *Response) Raw() *http.Response

Raw 获取原始的http response

func (*Response) String

func (resp *Response) String() (string, error)

String 读取http.Body, 返回string

func (*Response) WriteFile

func (resp *Response) WriteFile(filename string) (int64, error)

WriteFile 读取http.Body内容并写入文件中

func (*Response) WriteTo

func (resp *Response) WriteTo(w io.Writer) (int64, error)

WriteTo 读取http.Body并写入w中

Example
content := []byte("write file")
handler := func(rw http.ResponseWriter, req *http.Request) {
	rw.Write(content)
}
s := httptest.NewServer(http.HandlerFunc(handler))
defer s.Close()

req := NewRequest()
resp, err := req.Get(s.URL, nil, nil)
if err != nil {
	panic(err)
}
_, err = resp.WriteTo(os.Stdout)
if err != nil {
	panic(err)
}
Output:

write file

Jump to

Keyboard shortcuts

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