httpreq

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 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 HttpReq

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

HttpReq an simple http requester.

func New

func New(baseURL ...string) *HttpReq

New instance with base URL

func (*HttpReq) BaseURL

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

BaseURL with base URL

func (*HttpReq) BeforeSend

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

BeforeSend add callback before send.

func (*HttpReq) BytesBody

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

BytesBody with custom bytes body

func (*HttpReq) Client

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

Client custom http client

func (*HttpReq) ContentType

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

ContentType with custom content-Type header.

func (*HttpReq) JSONBytesBody added in v0.4.5

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

JSONBytesBody with custom bytes body, and set JSON content type

func (*HttpReq) Method

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

Method with custom method

func (*HttpReq) MustSend

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

MustSend request, will panic on error

func (*HttpReq) Send

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

Send request and return http response

func (*HttpReq) StringBody

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

StringBody with custom string body

func (*HttpReq) WithBody

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

WithBody with custom body

func (*HttpReq) WithHeader

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

WithHeader with custom header

func (*HttpReq) WithHeaders

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

WithHeaders with custom headers

Jump to

Keyboard shortcuts

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