util

package
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2023 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

The `util` package provides utility methods for decoding request parameters and bodies, and providing responses over HTTP

Index

Constants

View Source
const (
	ContentTypeKey            = "Content-Type"
	ContentLengthKey          = "Content-Length"
	ContentTypeJSON           = "application/json"
	ContentTypeText           = "text/plain"
	ContentTypeMultipartForm  = "multipart/form-data"
	ContentTypeUrlEncodedForm = "application/x-www-form-urlencoded"
)

Variables

This section is empty.

Functions

func ReqDecodeBody

func ReqDecodeBody(req *http.Request, v interface{}) ([]string, error)

Decode a request body into a struct, and return the fields which were decoded.

func ReqPrefixPathParams

func ReqPrefixPathParams(req *http.Request) (string, string, []string)

Return prefix and parameters from a request

func RequestBodyForm

func RequestBodyForm(req *http.Request, params map[string]string, v interface{}) ([]string, error)

RequestBodyForm returns the body of a request as a struct where the request is of type multipart/form-data

func RequestBodyJSON

func RequestBodyJSON(req *http.Request, v interface{}) ([]string, error)

RequestBodyJSON returns the body of a request as a struct where the request is of type application/json

func RequestBodyPost

func RequestBodyPost(req *http.Request, v interface{}) ([]string, error)

RequestBodyPost returns the body of a request as a struct where the request is of type application/x-www-form-urlencoded

func ServeEmpty

func ServeEmpty(w http.ResponseWriter, code uint)

ServeEmpty is a utility function to serve an empty response

func ServeError

func ServeError(w http.ResponseWriter, code uint, reason ...string) error

ServeError is a utility function to serve a JSON error notice

func ServeJSON

func ServeJSON(w http.ResponseWriter, v interface{}, code, indent uint) error

ServeJSON is a utility function to serve an arbitary object as JSON

func ServeText

func ServeText(w http.ResponseWriter, v string, code uint)

ServeText is a utility function to serve plaintext

Types

type ErrorResponse

type ErrorResponse struct {
	Code   uint   `json:"code"`
	Reason string `json:"reason,omitempty"`
}

ErrorResponse is a generic error response which is served as JSON using the ServeError method

Jump to

Keyboard shortcuts

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