httputil

package
v1.80.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2023 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package httputil contains a collection of HTTP request and response utility functions.

Index

Constants

View Source
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"
)
View Source
const (
	StatusSuccess = "success"
	StatusFail    = "fail"
	StatusError   = "error"
)

JSend status codes.

View Source
const ReqTimeCtxKey = timeCtxKey("request_time")

ReqTimeCtxKey is the Context key to retrieve the request time.

View Source
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

func AddBasicAuth(apiKey, apiSecret string, r *http.Request)

AddBasicAuth decorates the provided http.Request with Basic Authorization.

func GetRequestTime

func GetRequestTime(r *http.Request) (time.Time, bool)

GetRequestTime returns the request time from the http request.

func GetRequestTimeFromContext

func GetRequestTimeFromContext(ctx context.Context) (time.Time, bool)

GetRequestTimeFromContext returns the request time from the context.

func HeaderOrDefault

func HeaderOrDefault(r *http.Request, key string, defaultValue string) string

HeaderOrDefault returns the value of an HTTP header or a default value.

func Link(url, template string, segments ...any) string

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 PathParam

func PathParam(r *http.Request, name string) string

PathParam returns the value from the named path segment.

func QueryIntOrDefault

func QueryIntOrDefault(q url.Values, key string, defaultValue int) int

QueryIntOrDefault returns the integer value of the specified URL query parameter or a default value.

func QueryStringOrDefault

func QueryStringOrDefault(q url.Values, key string, defaultValue string) string

QueryStringOrDefault returns the string value of the specified URL query parameter or a default value.

func QueryUintOrDefault

func QueryUintOrDefault(q url.Values, key string, defaultValue uint) uint

QueryUintOrDefault returns the unsigned integer value of the specified URL query parameter or a default value.

func SendJSON

func SendJSON(ctx context.Context, w http.ResponseWriter, statusCode int, data any)

SendJSON sends a JSON object to the response.

func SendStatus

func SendStatus(ctx context.Context, w http.ResponseWriter, statusCode int)

SendStatus sends write a HTTP status code to the response.

func SendText

func SendText(ctx context.Context, w http.ResponseWriter, statusCode int, data string)

SendText sends text to the response.

func SendXML

func SendXML(ctx context.Context, w http.ResponseWriter, statusCode int, xmlHeader string, data any)

SendXML sends an XML object to the response.

func WithRequestTime

func WithRequestTime(ctx context.Context, t time.Time) context.Context

WithRequestTime returns a new context with the added request time.

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(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.

type Status

type Status int

Status translates the HTTP status code to a JSend status string.

func (Status) MarshalJSON

func (sc Status) MarshalJSON() ([]byte, error)

MarshalJSON implements the custom marshaling function for the json encoder.

Directories

Path Synopsis
Package jsendx implements a custom JSEND model to wrap HTTP responses in a JSON object with default fields.
Package jsendx implements a custom JSEND model to wrap HTTP responses in a JSON object with default fields.

Jump to

Keyboard shortcuts

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