http_client

package module
v0.0.0-...-1aa7e19 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2021 License: Apache-2.0 Imports: 15 Imported by: 0

README

http-client

http client

Documentation

Index

Constants

View Source
const (
	GET     = "GET"
	POST    = "POST"
	PUT     = "PUT"
	HEAD    = "HEAD"
	PATCH   = "PATCH"
	DELETE  = "DELETE"
	OPTIONS = "OPTIONS"

	ContentTypeJson = "application/json"
	ContentType     = "Content-Type"
	ContentTypeForm = "application/x-www-form-urlencoded"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Body

type Body interface {
	Encode(receiver io.ReadWriter) (string, error)
}

func NewFileBody

func NewFileBody(fileName, fieldName string, body ...interface{}) Body

func NewFormBody

func NewFormBody(body interface{}) Body

formBodyProvider encodes a url tagged struct value as Body for requests. See https://godoc.org/github.com/google/go-querystring/query for details.

func NewJsonBody

func NewJsonBody(body interface{}, escapeHTML ...bool) Body

jsonBodyProvider encodes a JSON tagged struct value as a Body for requests. See https://golang.org/pkg/encoding/json/#MarshalIndent for details.

func NewOriginalBody

func NewOriginalBody(body io.Reader) Body

bodyOriginal provides the wrapped body value as a Body for requests.

type Config

type Config struct {
	HTTPTimeout     HTTPTimeout // HTTP的超时时间设置
	UseProxy        bool        // 是否使用代理
	ProxyHost       string      // 代理服务器地址
	IsAuthProxy     bool        // 代理服务器是否使用用户认证
	ProxyUser       string      // 代理服务器认证用户名
	ProxyPassword   string      // 代理服务器认证密码
	ReUseTCP        bool        // 为同一地址多次请求复用TCP连接
	TLSClientConfig *tls.Config // tls config
}

Config configure

func NewConfig

func NewConfig() Config

获取默认配置

type HTTPTimeout

type HTTPTimeout struct {
	ConnectTimeout        time.Duration
	ReadTimeout           time.Duration
	WriteTimeout          time.Duration
	ResponseHeaderTimeout time.Duration
	MaxTimeout            time.Duration
	TLSHandshakeTimeout   time.Duration
}

HTTPTimeout http timeout

type HttpClient

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

func New

func New(cfg ...Config) *HttpClient

func (*HttpClient) AddQueries

func (h *HttpClient) AddQueries(params ...interface{}) *HttpClient

AddQuery add queries for GET request

func (*HttpClient) BaseURL

func (h *HttpClient) BaseURL(rawUrl string) *HttpClient

func (*HttpClient) Delete

func (h *HttpClient) Delete(pathURL string) *HttpClient

Delete sets the Sling method to DELETE and sets the given pathURL.

func (*HttpClient) Get

func (h *HttpClient) Get(pathURL string) *HttpClient

Get sets the Request method to GET and sets the given pathURL.

func (*HttpClient) Head

func (h *HttpClient) Head(pathURL string) *HttpClient

Head sets the Request method to HEAD and sets the given pathURL.

func (*HttpClient) New

func (h *HttpClient) New() *HttpClient

func (*HttpClient) Options

func (h *HttpClient) Options(pathURL string) *HttpClient

Options sets the Sling method to OPTIONS and sets the given pathURL.

func (*HttpClient) Patch

func (h *HttpClient) Patch(pathURL string) *HttpClient

Patch sets the Request method to PATCH and sets the given pathURL.

func (*HttpClient) Post

func (h *HttpClient) Post(pathURL string) *HttpClient

Post sets the Request method to POST and sets the given pathURL.

func (*HttpClient) Put

func (h *HttpClient) Put(pathURL string) *HttpClient

Put sets the Request method to PUT and sets the given pathURL.

func (*HttpClient) Send

func (h *HttpClient) Send(receiver ...io.ReadWriter) (statusCode int, err error)

func (*HttpClient) SetBasicAuth

func (h *HttpClient) SetBasicAuth(username, password string) *HttpClient

SetBasicAuth sets the Authorization header to use HTTP Basic Authentication with the provided username and password. With HTTP Basic Authentication the provided username and password are not encrypted.

func (*HttpClient) SetBody

func (h *HttpClient) SetBody(body Body) *HttpClient

func (*HttpClient) SetHeader

func (h *HttpClient) SetHeader(key, value string) *HttpClient

SetHeader sets the key, value pair in Headers, replacing existing values associated with key. Header keys are canonicalized.

type Retry

type Retry struct {
	Times int
	// if set to true , the status code greater than 400 will resend request
	// if set to false, the client only check the request error
	CheckStatusCode bool
}

Jump to

Keyboard shortcuts

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