httpclient

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2018 License: Apache-2.0 Imports: 12 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 Request

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

Request http请求 NOTICE: 不支持并发调用

func NewRequest

func NewRequest() *Request

NewRequest 创建request

func (*Request) DisableKeepAlive

func (req *Request) DisableKeepAlive() *Request

DisableKeepAlive 关闭连接重用

func (*Request) EnableDebug

func (req *Request) EnableDebug() *Request

EnableDebug 开启调试模式

func (*Request) EnableDefaultHeader

func (req *Request) EnableDefaultHeader() *Request

EnableDefaultHeader 自动设置默认header

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

func (*Request) SetClient

func (req *Request) SetClient(client *http.Client) *Request

SetClient 自定义http client

func (*Request) SetProxy

func (req *Request) SetProxy(proxyURL string) *Request

SetProxy 设置代理

func (*Request) SetRetryTimes

func (req *Request) SetRetryTimes(retryTimes int) *Request

SetRetryTimes 设置重试次数

func (*Request) SetShouldRetryFunc

func (req *Request) SetShouldRetryFunc(f func(*http.Response, error) bool) *Request

SetShouldRetryFunc 设置是否应该重试方法

func (*Request) SetTimeout

func (req *Request) SetTimeout(timeout time.Duration) *Request

SetTimeout 设置超时

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