httpreq

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2023 License: MIT Imports: 12 Imported by: 8

Documentation

Overview

Package httpreq an simple http requester

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddHeaderMap added in v0.6.8

func AddHeaderMap(req *http.Request, headerMap map[string]string)

AddHeaderMap to reqeust instance.

func AddHeaders added in v0.5.12

func AddHeaders(req *http.Request, header http.Header)

AddHeaders adds the key, value pairs from the given http.Header to the request. Values for existing keys are appended to the keys values.

func AppendQueryToURL added in v0.6.8

func AppendQueryToURL(reqURL *url.URL, uv url.Values) error

AppendQueryToURL appends the given query string to the given url.

func AppendQueryToURLString added in v0.6.8

func AppendQueryToURLString(urlStr string, query url.Values) string

AppendQueryToURLString appends the given query data to the given url.

func BuildBasicAuth

func BuildBasicAuth(username, password string) string

BuildBasicAuth returns the base64 encoded username:password for basic auth. Then set to header "Authorization".

copied from net/http.

func ConfigStd added in v0.6.0

func ConfigStd(fn func(hc *http.Client))

ConfigStd req client

func Get added in v0.6.0

func Get(url string, opt *ReqOption) (*http.Response, error)

Get quick send a GET request by default client

func HeaderToString added in v0.6.8

func HeaderToString(h http.Header) string

HeaderToString convert http Header to string

func HeaderToStringMap added in v0.6.0

func HeaderToStringMap(rh http.Header) map[string]string

HeaderToStringMap convert

func IsClientError

func IsClientError(statusCode int) bool

IsClientError check response is client error (400 - 500)

func IsForbidden

func IsForbidden(statusCode int) bool

IsForbidden is this response forbidden(403)

func IsNoBodyMethod added in v0.6.8

func IsNoBodyMethod(method string) bool

IsNoBodyMethod check

func IsNotFound

func IsNotFound(statusCode int) bool

IsNotFound is this response not found(404)

func IsOK

func IsOK(statusCode int) bool

IsOK check response status code is 200

func IsRedirect

func IsRedirect(statusCode int) bool

IsRedirect check response status code is in [301, 302, 303, 307]

func IsServerError

func IsServerError(statusCode int) bool

IsServerError check response is server error (500 - 600)

func IsSuccessful

func IsSuccessful(statusCode int) bool

IsSuccessful check response status code is in 200 - 300

func Post added in v0.6.0

func Post(url string, data any, opt *ReqOption) (*http.Response, error)

Post quick send a POS request by default client

func RequestToString added in v0.4.5

func RequestToString(r *http.Request) string

RequestToString convert http Request to string

func ResponseToString added in v0.4.5

func ResponseToString(w *http.Response) string

ResponseToString convert http Response to string

func ToQueryValues

func ToQueryValues(data any) url.Values

ToQueryValues convert string-map or any-map to url.Values

func ToRequestBody added in v0.6.8

func ToRequestBody(data any) io.Reader

ToRequestBody convert handle

Allow type for data:

  • string
  • []byte
  • map[string]string
  • map[string][]string/url.Values
  • io.Reader(eg: bytes.Buffer, strings.Reader)

Types

type BasicAuthConf added in v0.6.8

type BasicAuthConf struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

BasicAuthConf struct

func (*BasicAuthConf) IsValid added in v0.6.8

func (ba *BasicAuthConf) IsValid() bool

IsValid value

func (*BasicAuthConf) String added in v0.6.8

func (ba *BasicAuthConf) String() string

String build to auth header "Authorization".

func (*BasicAuthConf) Value added in v0.6.8

func (ba *BasicAuthConf) Value() string

Value build to auth header "Authorization".

type Doer

type Doer interface {
	Do(req *http.Request) (*http.Response, error)
}

Doer interface for http client.

type DoerFunc

type DoerFunc func(req *http.Request) (*http.Response, error)

DoerFunc implements the Doer

func (DoerFunc) Do

func (do DoerFunc) Do(req *http.Request) (*http.Response, error)

Do send request and return response.

type Req deprecated added in v0.5.5

type Req = ReqClient

Req alias of ReqClient

Deprecated: rename to ReqClient

type ReqClient added in v0.6.0

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

ReqClient an simple http request client.

func New

func New(baseURL ...string) *ReqClient

New instance with base URL

func Std added in v0.6.8

func Std() *ReqClient

Std instance

func (*ReqClient) AnyBody added in v0.6.0

func (h *ReqClient) AnyBody(data any) *ReqClient

AnyBody with custom body.

Allow type:

  • string, []byte, map[string][]string/url.Values, io.Reader(eg: bytes.Buffer, strings.Reader)

func (*ReqClient) BaseURL added in v0.6.0

func (h *ReqClient) BaseURL(baseURL string) *ReqClient

BaseURL with base URL

func (*ReqClient) BytesBody added in v0.6.0

func (h *ReqClient) BytesBody(bs []byte) *ReqClient

BytesBody with custom bytes body

func (*ReqClient) Client added in v0.6.0

func (h *ReqClient) Client(c Doer) *ReqClient

Client custom http client

func (*ReqClient) ContentType added in v0.6.0

func (h *ReqClient) ContentType(cType string) *ReqClient

ContentType with custom content-Type header.

func (*ReqClient) Doer added in v0.6.0

func (h *ReqClient) Doer() Doer

Doer get the http client

func (*ReqClient) JSONBytesBody added in v0.6.0

func (h *ReqClient) JSONBytesBody(bs []byte) *ReqClient

JSONBytesBody with custom bytes body, and set JSON content type

func (*ReqClient) Method added in v0.6.0

func (h *ReqClient) Method(method string) *ReqClient

Method with custom method

func (*ReqClient) MustSend added in v0.6.0

func (h *ReqClient) MustSend(url string) *http.Response

MustSend request, will panic on error

func (*ReqClient) OnAfterSend added in v0.6.8

func (h *ReqClient) OnAfterSend(fn func(resp *http.Response)) *ReqClient

OnAfterSend add callback after send.

func (*ReqClient) OnBeforeSend added in v0.6.8

func (h *ReqClient) OnBeforeSend(fn func(req *http.Request)) *ReqClient

OnBeforeSend add callback before send.

func (*ReqClient) Send added in v0.6.0

func (h *ReqClient) Send(url string) (*http.Response, error)

Send request and return http response

func (*ReqClient) SendRequest added in v0.6.8

func (h *ReqClient) SendRequest(req *http.Request, opt *ReqOption) (*http.Response, error)

SendRequest request and return http response

func (*ReqClient) SendWithOpt added in v0.6.0

func (h *ReqClient) SendWithOpt(url string, opt *ReqOption) (*http.Response, error)

SendWithOpt request and return http response

func (*ReqClient) StringBody added in v0.6.0

func (h *ReqClient) StringBody(s string) *ReqClient

StringBody with custom string body

func (*ReqClient) WithBody added in v0.6.0

func (h *ReqClient) WithBody(r io.Reader) *ReqClient

WithBody with custom body

func (*ReqClient) WithHeader added in v0.6.0

func (h *ReqClient) WithHeader(key, val string) *ReqClient

WithHeader with custom header

func (*ReqClient) WithHeaders added in v0.6.0

func (h *ReqClient) WithHeaders(kvMap map[string]string) *ReqClient

WithHeaders with custom headers

type ReqLogger added in v0.6.0

type ReqLogger interface {
	Infof(format string, args ...any)
	Errorf(format string, args ...any)
}

ReqLogger interface

type ReqOption added in v0.6.0

type ReqOption struct {
	// Method for request
	Method string
	// HeaderMap data. eg: traceid
	HeaderMap map[string]string
	// Timeout unit: ms
	Timeout int
	// TCancelFunc will auto set it on Timeout > 0
	TCancelFunc context.CancelFunc
	// ContentType header
	ContentType string
	// EncodeJSON req body
	EncodeJSON bool
	// Logger for request
	Logger ReqLogger
	// Context for request
	Context context.Context
}

ReqOption struct

type Resp added in v0.6.0

type Resp struct {
	*http.Response
	// CostTime for a request-response
	CostTime int64
}

Resp struct

func NewResp added in v0.6.0

func NewResp(hr *http.Response) *Resp

NewResp instance

func (*Resp) BindJSON added in v0.6.1

func (r *Resp) BindJSON(ptr any) error

BindJSON body data to a ptr

NOTICE: must close resp body.

func (*Resp) BindJSONOnOk added in v0.6.1

func (r *Resp) BindJSONOnOk(ptr any) error

BindJSONOnOk body data on status is 200

NOTICE: must close resp body.

func (*Resp) BodyBuffer added in v0.6.0

func (r *Resp) BodyBuffer() *bytes.Buffer

BodyBuffer read body to buffer.

NOTICE: must close resp body.

func (*Resp) BodyString added in v0.6.0

func (r *Resp) BodyString() string

BodyString get body as string.

func (*Resp) CloseBody added in v0.6.0

func (r *Resp) CloseBody() error

CloseBody close resp body

func (*Resp) ContentType added in v0.6.0

func (r *Resp) ContentType() string

ContentType get response content type

func (*Resp) IsEmptyBody added in v0.6.0

func (r *Resp) IsEmptyBody() bool

IsEmptyBody check response body is empty

func (*Resp) IsFail added in v0.6.0

func (r *Resp) IsFail() bool

IsFail check

func (*Resp) IsOk added in v0.6.0

func (r *Resp) IsOk() bool

IsOk check

func (*Resp) IsSuccessful added in v0.6.0

func (r *Resp) IsSuccessful() bool

IsSuccessful check

func (*Resp) QuiteCloseBody added in v0.6.0

func (r *Resp) QuiteCloseBody()

QuiteCloseBody close resp body, ignore error

func (*Resp) String added in v0.6.0

func (r *Resp) String() string

BodyString get body as string.

Jump to

Keyboard shortcuts

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