httpx

package module
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2023 License: MIT Imports: 11 Imported by: 0

README

httpx

build-img pkg-img reportcard-img coverage-img version-img

Better net/http for Go.

Rationale

TODO

Features

  • Simple.

See GUIDE.md for more details.

Install

Go version 1.17+

go get github.com/cristalhq/httpx

Example

TODO

Documentation

See these docs.

License

MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoBody = http.NoBody

Functions

func Bearer added in v0.4.3

func Bearer(token string) string

Bearer header with a give token.

func DiscardResponseBody added in v0.4.3

func DiscardResponseBody(resp *http.Response)

DiscardResponseBody reads and closes http.Response.Body.

func DumpRequest added in v0.4.5

func DumpRequest(r *http.Request) string

func ErrorResponse added in v0.1.2

func ErrorResponse(w http.ResponseWriter, code int, err error)

ErrorResponse return and error wrapped into JSON. If the err is httpx.Error, code parameter is ignoted and httpx.Error.Code is used. If the err is nil just status code is returned.

func Is1xx added in v0.4.3

func Is1xx(code int) bool

func Is2xx added in v0.4.3

func Is2xx(code int) bool

func Is3xx added in v0.4.3

func Is3xx(code int) bool

func Is4xx added in v0.4.3

func Is4xx(code int) bool

func Is5xx added in v0.4.3

func Is5xx(code int) bool

func MarshalResponse

func MarshalResponse(w http.ResponseWriter, status int, response interface{})

MarshalResponse writes an object to the http.ResponseWriter or a fallback error.

func MustGetRequest added in v0.4.2

func MustGetRequest(ctx context.Context, url string) *http.Request

MustGetRequest returns a new http.Request with GET method or panics on error.

func MustNewRequest

func MustNewRequest(ctx context.Context, method, url string, body io.Reader) *http.Request

MustNewRequest returns a new http.Request or panics on error.

func MustPostRequest added in v0.4.2

func MustPostRequest(ctx context.Context, url string, body io.Reader) *http.Request

MustPostRequest returns a new http.Request with POST method or panics on error.

func MustPutRequest added in v0.4.4

func MustPutRequest(ctx context.Context, url string, body io.Reader) *http.Request

MustPutRequest returns a new http.Request with PUT method or panics on error.

func NewClient added in v0.4.1

func NewClient() *http.Client

NewClient return http.Client with a sane default.

func NewConnectRequest added in v0.4.2

func NewConnectRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error)

NewConnectRequest returns a new http.Request with CONNECT method.

func NewDeleteRequest added in v0.4.2

func NewDeleteRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error)

NewDeleteRequest returns a new http.Request with DELETE method.

func NewGetRequest added in v0.2.0

func NewGetRequest(ctx context.Context, url string) (*http.Request, error)

NewGetRequest returns a new http.Request with GET method.

func NewHeadRequest added in v0.4.2

func NewHeadRequest(ctx context.Context, url string) (*http.Request, error)

NewHeadRequest returns a new http.Request with HEAD method.

func NewOptionsRequest added in v0.4.2

func NewOptionsRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error)

NewOptionsRequest returns a new http.Request with OPTIONS method.

func NewPatchRequest added in v0.4.2

func NewPatchRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error)

NewPatchRequest returns a new http.Request with PATCH method.

func NewPostRequest added in v0.2.0

func NewPostRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error)

NewPostRequest returns a new http.Request with POST method.

func NewPutRequest added in v0.4.2

func NewPutRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error)

NewPutRequest returns a new http.Request with PUT method.

func NewRequest

func NewRequest(ctx context.Context, method, url string, body io.Reader) (*http.Request, error)

NewRequest returns a new http.Request.

func NewTraceRequest added in v0.4.2

func NewTraceRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error)

NewTraceRequest returns a new http.Request with TRACE method.

func NoopHandler added in v0.4.5

func NoopHandler(http.ResponseWriter, *http.Request)

NoopHandler just return 200 OK. Useful for prototyping.

func ReturnBadRequest added in v0.2.0

func ReturnBadRequest(w http.ResponseWriter, err error)

func ReturnNotFound added in v0.2.0

func ReturnNotFound(w http.ResponseWriter)

func ReturnOK added in v0.2.0

func ReturnOK(w http.ResponseWriter)

func ReturnOKJSON added in v0.3.3

func ReturnOKJSON(w http.ResponseWriter, data interface{})

func ReturnRedirect added in v0.2.0

func ReturnRedirect(w http.ResponseWriter)

func UnmarshalRequest

func UnmarshalRequest(w http.ResponseWriter, r *http.Request, data interface{}) (code int, err error)

UnmarshalRequest as JSON with well defined error handling.

Types

type Doer added in v0.1.1

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

Doer is an interface for http.Client.

type Error added in v0.1.3

type Error struct {
	Code    int    `json:"code,omitempty"`
	Type    string `json:"type,omitempty"`
	Message string `json:"msg,omitempty"`
}

Error for a request.

func (Error) Error added in v0.2.0

func (e Error) Error() string

type Server added in v0.1.4

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

Server for HTTP protocol.

func NewServer added in v0.1.4

func NewServer(config *ServerConfig) (*Server, error)

NewServer returns a new Server.

func (*Server) Run added in v0.1.4

func (s *Server) Run(ctx context.Context) error

Run starts the server.

func (*Server) Start added in v0.4.0

func (s *Server) Start(ctx context.Context, h http.Handler) error

Start the server with a given handler. Same as Run but allows to set handler later.

type ServerConfig added in v0.1.4

type ServerConfig struct {
	Addr    string
	Handler http.Handler

	NoHTTP2   bool
	TLSConfig *tls.Config

	// TODO: enable option
	HeartbeatPath    string
	HeartbeatHandler http.HandlerFunc

	ReadTimeout       time.Duration
	ReadHeaderTimeout time.Duration
	WriteTimeout      time.Duration
	IdleTimeout       time.Duration
	MaxHeaderBytes    int
}

ServerConfig configures Server.

func (*ServerConfig) Validate added in v0.1.4

func (c *ServerConfig) Validate() error

Validate the config.

Jump to

Keyboard shortcuts

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