httpx

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 20 Imported by: 0

README

httpx

build-img pkg-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 NilRequest = &http.Request{
	Method: "STUB",
	URL:    &url.URL{Path: "/"},
	Header: http.Header{
		"X-Real-Ip": []string{"0.0.0.0"},
	},
}
View Source
var NoBody = http.NoBody

Functions

func AcceptLanguage added in v0.5.2

func AcceptLanguage(req *http.Request) string

AcceptLanguage returns the highest-preference language from the 'Accept-Language' header. If the header is missing or empty, it defaults to "en".

func Bearer added in v0.4.3

func Bearer(token string) string

Bearer header with a give token.

func ContentTypeByExt added in v0.5.1

func ContentTypeByExt(file string) string

ContentTypeByExt or 'application/octet-stream' if not found.

func DiscardResponse added in v0.6.0

func DiscardResponse(resp *http.Response)

DiscardResponse 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 any)

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 returns http.Client with a sane defaults and non-shared http.Transport. See NewTransport for more info.

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

func NewPooledClient() *http.Client

NewPooledClient returns http.Client which will be used for the same host(s).

func NewPooledTransport added in v0.5.1

func NewPooledTransport() *http.Transport

NewPooledTransport returns http.Transport which will be used for the same host(s).

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

func NewTransport() *http.Transport

NewTransport returns http.Transport with idle connections and keepalives disabled.

func NoopHandler added in v0.4.5

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

NoopHandler just return 200 OK. Useful for prototyping.

func RequestIP added in v0.5.1

func RequestIP(r *http.Request) netip.Addr

RequestIP from the headers if possible.

func RequestIPRaw added in v0.6.0

func RequestIPRaw(r *http.Request) string

RequestIPRaw from the headers if possible. Fallbacks to http.Request.RemoteAddr.

func ResponseJSON added in v0.6.0

func ResponseJSON(w http.ResponseWriter, data any)

func ResponseOK added in v0.6.0

func ResponseOK(w http.ResponseWriter)

func ServeFile added in v0.6.0

func ServeFile(fs fs.FS, file string) http.HandlerFunc

func UnmarshalRequest

func UnmarshalRequest(w http.ResponseWriter, r *http.Request, maxBodyBytes int64, data any) (code int, err error)

UnmarshalRequest as JSON with well defined error handling.

Types

type Client added in v0.5.1

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

Client is an interface for http.Client.

type Doer added in v0.1.1

type Doer = Client

Doer is a backward compatible definition for 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 LangQ added in v0.5.2

type LangQ struct {
	Lang string  // The language tag (e.g., "en", "fr", "en-us").
	Q    float64 // The quality factor, ranging from 0 to 1.
}

LangQ represents a language and its quality factor (q-value) from the HTTP Accept-Language header.

func AcceptLanguages added in v0.5.2

func AcceptLanguages(req *http.Request) []LangQ

AcceptLanguages parses the 'Accept-Language' header and returns a slice of LangQ sorted by descending quality factor. If no valid languages are found, it returns a default slice containing {"en", 1}.

type Router added in v0.5.0

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

Router for net/http.

func NewRouter added in v0.5.0

func NewRouter() *Router

NewRouter creates a new Router

func (*Router) Group added in v0.5.0

func (r *Router) Group(fn func(r *Router))

Group creates a new subrouter inheriting global middlewares.

func (*Router) Handle added in v0.5.0

func (r *Router) Handle(pattern string, h http.Handler)

Handle add pattern to the router or subrouter.

func (*Router) HandleFunc added in v0.5.0

func (r *Router) HandleFunc(pattern string, h http.HandlerFunc)

HandleFunc add pattern to the router or subrouter.

func (*Router) ServeHTTP added in v0.5.0

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements http.Handler

func (*Router) Use added in v0.5.0

func (r *Router) Use(mws ...func(http.Handler) http.Handler)

Use given middlewares for the current router or subrouter.

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
	CertFile  string
	KeyFile   string

	// 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