httpx

package
v1.6.4 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: MIT Imports: 16 Imported by: 292

Documentation

Index

Constants

View Source
const (
	// ContentEncoding means Content-Encoding.
	ContentEncoding = "Content-Encoding"
	// ContentSecurity means X-Content-Security.
	ContentSecurity = "X-Content-Security"
	// ContentType means Content-Type.
	ContentType = header.ContentType
	// JsonContentType means application/json.
	JsonContentType = header.JsonContentType
	// KeyField means key.
	KeyField = "key"
	// SecretField means secret.
	SecretField = "secret"
	// TypeField means type.
	TypeField = "type"
	// CryptionType means cryption.
	CryptionType = 1
)
View Source
const (
	// CodeSignaturePass means signature verification passed.
	CodeSignaturePass = iota
	// CodeSignatureInvalidHeader means invalid header in signature.
	CodeSignatureInvalidHeader
	// CodeSignatureWrongTime means wrong timestamp in signature.
	CodeSignatureWrongTime
	// CodeSignatureInvalidToken means invalid token in signature.
	CodeSignatureInvalidToken
)

Variables

This section is empty.

Functions

func Error

func Error(w http.ResponseWriter, err error, fns ...func(w http.ResponseWriter, err error))

Error writes err into w.

func ErrorCtx added in v1.4.3

func ErrorCtx(ctx context.Context, w http.ResponseWriter, err error,
	fns ...func(w http.ResponseWriter, err error))

ErrorCtx writes err into w.

func GetFormValues added in v1.4.0

func GetFormValues(r *http.Request) (map[string]any, error)

GetFormValues returns the form values.

func GetRemoteAddr

func GetRemoteAddr(r *http.Request) string

GetRemoteAddr returns the peer address, supports X-Forward-For.

func Ok

func Ok(w http.ResponseWriter)

Ok writes HTTP 200 OK into w.

func OkJson

func OkJson(w http.ResponseWriter, v any)

OkJson writes v into w with 200 OK.

func OkJsonCtx added in v1.4.3

func OkJsonCtx(ctx context.Context, w http.ResponseWriter, v any)

OkJsonCtx writes v into w with 200 OK.

func Parse

func Parse(r *http.Request, v any) error

Parse parses the request.

func ParseForm

func ParseForm(r *http.Request, v any) error

ParseForm parses the form request.

func ParseHeader

func ParseHeader(headerValue string) map[string]string

ParseHeader parses the request header and returns a map.

func ParseHeaders

func ParseHeaders(r *http.Request, v any) error

ParseHeaders parses the headers request.

func ParseJsonBody

func ParseJsonBody(r *http.Request, v any) error

ParseJsonBody parses the post request which contains json in body.

func ParsePath

func ParsePath(r *http.Request, v any) error

ParsePath parses the symbols reside in url path. Like http://localhost/bag/:name

func SetErrorHandler

func SetErrorHandler(handler func(error) (int, any))

SetErrorHandler sets the error handler, which is called on calling Error. Notice: SetErrorHandler and SetErrorHandlerCtx set the same error handler. Keeping both SetErrorHandler and SetErrorHandlerCtx is for backward compatibility.

func SetErrorHandlerCtx added in v1.4.3

func SetErrorHandlerCtx(handlerCtx func(context.Context, error) (int, any))

SetErrorHandlerCtx sets the error handler, which is called on calling Error. Notice: SetErrorHandler and SetErrorHandlerCtx set the same error handler. Keeping both SetErrorHandler and SetErrorHandlerCtx is for backward compatibility.

func SetOkHandler added in v1.5.4

func SetOkHandler(handler func(context.Context, any) any)

SetOkHandler sets the response handler, which is called on calling OkJson and OkJsonCtx.

func SetValidator added in v1.4.5

func SetValidator(val Validator)

SetValidator sets the validator. The validator is used to validate the request, only called in Parse, not in ParseHeaders, ParseForm, ParseHeader, ParseJsonBody, ParsePath.

func Stream added in v1.6.4

func Stream(ctx context.Context, w http.ResponseWriter, fn func(w io.Writer) bool)

Stream writes data into w with streaming mode. The ctx is used to control the streaming loop, typically use r.Context(). The fn is called repeatedly until it returns false.

func WriteJson

func WriteJson(w http.ResponseWriter, code int, v any)

WriteJson writes v as json string into w with code.

func WriteJsonCtx added in v1.4.3

func WriteJsonCtx(ctx context.Context, w http.ResponseWriter, code int, v any)

WriteJsonCtx writes v as json string into w with code.

Types

type Router

type Router interface {
	http.Handler
	Handle(method, path string, handler http.Handler) error
	SetNotFoundHandler(handler http.Handler)
	SetNotAllowedHandler(handler http.Handler)
}

Router interface represents a http router that handles http requests.

type Validator added in v1.4.5

type Validator interface {
	// Validate validates the request and parsed data.
	Validate(r *http.Request, data any) error
}

Validator defines the interface for validating the request.

Jump to

Keyboard shortcuts

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