Documentation ¶
Overview ¶
Package httputil contains a collection of HTTP request and response utility functions.
Index ¶
- Constants
- func AddBasicAuth(apiKey, apiSecret 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 SendJSON(ctx context.Context, w http.ResponseWriter, statusCode int, data any)
- func SendStatus(ctx context.Context, w http.ResponseWriter, statusCode int)
- func SendText(ctx context.Context, w http.ResponseWriter, statusCode int, data string)
- func SendXML(ctx context.Context, w http.ResponseWriter, statusCode int, xmlHeader string, ...)
- func WithRequestTime(ctx context.Context, t time.Time) context.Context
- type ResponseWriterWrapper
- type Status
Constants ¶
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 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 SendStatus ¶
func SendStatus(ctx context.Context, w http.ResponseWriter, statusCode int)
SendStatus sends write a HTTP status code to the response.
Types ¶
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.