httpreq

package
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2022 License: MIT Imports: 7 Imported by: 8

Documentation

Overview

Package httpreq an simple http requester

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddHeadersToRequest

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

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

func BuildBasicAuth

func BuildBasicAuth(username, password string) string

BuildBasicAuth returns the base64 encoded username:password for basic auth. copied from net/http.

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 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 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 interface{}) url.Values

ToQueryValues convert string map to url.Values

Types

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 added in v0.5.5

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

Req an simple http requester.

func New

func New(baseURL ...string) *Req

New instance with base URL

func (*Req) BaseURL added in v0.5.5

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

BaseURL with base URL

func (*Req) BeforeSend added in v0.5.5

func (h *Req) BeforeSend(fn func(req *http.Request)) *Req

BeforeSend add callback before send.

func (*Req) BytesBody added in v0.5.5

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

BytesBody with custom bytes body

func (*Req) Client added in v0.5.5

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

Client custom http client

func (*Req) ContentType added in v0.5.5

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

ContentType with custom content-Type header.

func (*Req) JSONBytesBody added in v0.5.5

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

JSONBytesBody with custom bytes body, and set JSON content type

func (*Req) Method added in v0.5.5

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

Method with custom method

func (*Req) MustSend added in v0.5.5

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

MustSend request, will panic on error

func (*Req) Send added in v0.5.5

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

Send request and return http response

func (*Req) StringBody added in v0.5.5

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

StringBody with custom string body

func (*Req) WithBody added in v0.5.5

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

WithBody with custom body

func (*Req) WithHeader added in v0.5.5

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

WithHeader with custom header

func (*Req) WithHeaders added in v0.5.5

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

WithHeaders with custom headers

Jump to

Keyboard shortcuts

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