Documentation
¶
Overview ¶
Package httputil contains a collection of common HTTP Request and Response utility functions. The package is designed to be used in conjunction with the net/http package in the Go standard library. It includes functions for parsing query parameters, reading request bodies, and writing response bodies.
Index ¶
- Constants
- func AddBasicAuth(apiKey, apiSecret string, r *http.Request)
- func AddBearerToken(token string, r *http.Request)
- func GetRequestTime(r *http.Request) (time.Time, bool)
- func GetRequestTimeFromContext(ctx context.Context) (time.Time, bool)
- func HeaderOrDefault(r *http.Request, key string, defaultValue string) string
- func Link(url, template string, segments ...any) string
- func PathParam(r *http.Request, name string) string
- func QueryIntOrDefault(q url.Values, key string, defaultValue int) int
- func QueryStringOrDefault(q url.Values, key string, defaultValue string) string
- func QueryUintOrDefault(q url.Values, key string, defaultValue uint) uint
- func StringValueOrDefault(v, def string) string
- func WithRequestTime(ctx context.Context, t time.Time) context.Context
- type HTTPResp
- func (hr *HTTPResp) SendJSON(ctx context.Context, w http.ResponseWriter, statusCode int, data any)
- func (hr *HTTPResp) SendStatus(ctx context.Context, w http.ResponseWriter, statusCode int)
- func (hr *HTTPResp) SendText(ctx context.Context, w http.ResponseWriter, statusCode int, data string)
- func (hr *HTTPResp) SendXML(ctx context.Context, w http.ResponseWriter, statusCode int, xmlHeader string, ...)
- type ResponseWriterWrapper
- type Status
Constants ¶
const ( HeaderAuthorization = "Authorization" HeaderAuthBasic = "Basic " HeaderAuthBearer = "Bearer " HeaderContentType = "Content-Type" HeaderAccept = "Accept" MimeTypeJSON = "application/json" )
Common HTTP headers and MIME types.
const ( // MimeApplicationJSON contains the mime type string for JSON content. MimeApplicationJSON = "application/json; charset=utf-8" // MimeApplicationXML contains the mime type string for XML content. MimeApplicationXML = "application/xml; charset=utf-8" // MimeTextPlain contains the mime type string for text content. MimeTextPlain = "text/plain; charset=utf-8" )
const ( StatusSuccess = "success" StatusFail = "fail" StatusError = "error" )
JSend status codes.
const ReqTimeCtxKey = timeCtxKey("request_time")
ReqTimeCtxKey is the Context key to retrieve the request time.
const XMLHeader = xml.Header
XMLHeader is a default XML Declaration header suitable for use with the SendXML function.
Variables ¶
This section is empty.
Functions ¶
func AddBasicAuth ¶
AddBasicAuth decorates the provided http.Request with Basic Authorization.
func AddBearerToken ¶
AddBearerToken decorates the provided http.Request with Bearer Authorization.
func GetRequestTime ¶
GetRequestTime returns the request time from the http request.
func GetRequestTimeFromContext ¶
GetRequestTimeFromContext returns the request time from the context.
func HeaderOrDefault ¶
HeaderOrDefault returns the value of an HTTP header or a default value.
func Link ¶
Link generates a public Link using the service url. It replaces all segments into the template. The template then gets joined at the end of the service url.
func QueryIntOrDefault ¶
QueryIntOrDefault returns the integer value of the specified URL query parameter or a default value.
func QueryStringOrDefault ¶
QueryStringOrDefault returns the string value of the specified URL query parameter or a default value.
func QueryUintOrDefault ¶
QueryUintOrDefault returns the unsigned integer value of the specified URL query parameter or a default value.
func StringValueOrDefault ¶
StringValueOrDefault returns the string value or a default value.
Types ¶
type HTTPResp ¶ added in v1.111.1
type HTTPResp struct {
// contains filtered or unexported fields
}
HTTPResp holds the configuration for the HTTP response methods.
func NewHTTPResp ¶ added in v1.111.1
NewHTTPResp returns a new Response object.
func (*HTTPResp) SendStatus ¶ added in v1.111.1
SendStatus sends write a HTTP status code to the response.
type ResponseWriterWrapper ¶
type ResponseWriterWrapper interface {
http.ResponseWriter
// Size returns the total number of bytes sent to the client.
Size() int
// Status returns the HTTP status of the request.
Status() int
// Tee sets a writer that will contain a copy of the bytes written to the response writer.
Tee(w io.Writer)
}
ResponseWriterWrapper is the interface defining the extendend functions of the proxy.
func NewResponseWriterWrapper ¶
func NewResponseWriterWrapper(w http.ResponseWriter) ResponseWriterWrapper
NewResponseWriterWrapper wraps an http.ResponseWriter with an enhanced proxy.