httpclient

package
v0.0.0-...-9172e6a Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2023 License: MIT 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

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

DNSResolver DNS解析

type DialContext

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

type Option

type Option func(*options)

func WithClient

func WithClient(client *http.Client) Option

WithClient 自定义http client

func WithConnectTimeout

func WithConnectTimeout(timeout time.Duration) Option

WithConnectTimeout 设置连接超时

func WithCookieJar

func WithCookieJar(cookieJar http.CookieJar) Option

func WithDNSResolver

func WithDNSResolver(dnsResolver DNSResolverFunc) Option

func WithDebug

func WithDebug() Option

WithDebug 开启调试模式

func WithDisableKeepAlive

func WithDisableKeepAlive() Option

WithDisableKeepAlive 连接重用

func WithEnableDefaultHeader

func WithEnableDefaultHeader() Option

WithEnableDefaultHeader 设置默认header

func WithMaxIdleConnsPerHost

func WithMaxIdleConnsPerHost(n int) Option

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

func WithProxyURL

func WithProxyURL(proxyURL string) Option

WithProxyURL 设置代理

func WithRetryTime

func WithRetryTime(retryTimes int) Option

WithRetryTime 设置重试次数

func WithShouldRetryFunc

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

WithShouldRetryFunc 自定义是否需要重试

func WithTimeout

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