Documentation ¶
Overview ¶
Package httputil is a set of opinionated helpers for HTTP servers/handlers, working with HTTP requests and responses, and managing errors.
Helpers include e.g. extracting bearer tokens from HTTP headers, retrieving parameters from the query string or parsing the HTTP body into a JSON-based struct.
It is derived originally derived from Camlistore, which is now available at https://camlistore.org/pkg/httputil.
Index ¶
- func BadRequestError(w http.ResponseWriter, errorMessage string, args ...interface{})
- func BearerToken(r *http.Request) (string, bool)
- func CloseBody(rc io.ReadCloser)
- func DumpRequestOut(w io.Writer, r *http.Request)
- func EqualJSON(a, b []byte) bool
- func ForbiddenError(w http.ResponseWriter, errorMessage string, args ...interface{})
- func FormBool(r *http.Request, key string, defaultValue bool) bool
- func FormFloat64(r *http.Request, key string, defaultValue float64) float64
- func FormInt(r *http.Request, key string, defaultValue int) int
- func FormInt32(r *http.Request, key string, defaultValue int32) int32
- func FormInt64(r *http.Request, key string, defaultValue int64) int64
- func FormString(r *http.Request, key string, defaultValue string) string
- func InternalServerError(w http.ResponseWriter, r *http.Request, err interface{})
- func IsGetOrHead(r *http.Request) bool
- func IsWebsocketUpgrade(req *http.Request) bool
- func IsXHR(r *http.Request) bool
- func MustFormBool(r *http.Request, key string) bool
- func MustFormFloat64(r *http.Request, key string) float64
- func MustFormInt(r *http.Request, key string) int
- func MustFormInt32(r *http.Request, key string) int32
- func MustFormInt64(r *http.Request, key string) int64
- func MustFormString(r *http.Request, key string) string
- func MustParamsBool(r *http.Request, key string) bool
- func MustParamsFloat64(r *http.Request, key string) float64
- func MustParamsInt(r *http.Request, key string) int
- func MustParamsInt32(r *http.Request, key string) int32
- func MustParamsInt64(r *http.Request, key string) int64
- func MustParamsString(r *http.Request, key string) string
- func MustQueryBool(r *http.Request, key string) bool
- func MustQueryDuration(r *http.Request, key string) time.Duration
- func MustQueryDurationWithDefault(r *http.Request, key string, defaultValue time.Duration) time.Duration
- func MustQueryFloat64(r *http.Request, key string) float64
- func MustQueryInt(r *http.Request, key string) int
- func MustQueryInt32(r *http.Request, key string) int32
- func MustQueryInt64(r *http.Request, key string) int64
- func MustQueryString(r *http.Request, key string) string
- func MustQueryTime(r *http.Request, key, layout string) time.Time
- func MustQueryTimeWithDefault(r *http.Request, key, layout string, defaultValue time.Time) time.Time
- func MustReadJSON(r *http.Request, dst interface{})
- func ParamsBool(r *http.Request, key string, defaultValue bool) bool
- func ParamsFloat64(r *http.Request, key string, defaultValue float64) float64
- func ParamsInt(r *http.Request, key string, defaultValue int) int
- func ParamsInt32(r *http.Request, key string, defaultValue int32) int32
- func ParamsInt64(r *http.Request, key string, defaultValue int64) int64
- func ParamsString(r *http.Request, key string, defaultValue string) string
- func QueryBool(r *http.Request, key string, defaultValue bool) bool
- func QueryDuration(r *http.Request, key string) time.Duration
- func QueryDurationWithDefault(r *http.Request, key string, defaultValue time.Duration) time.Duration
- func QueryFloat64(r *http.Request, key string, defaultValue float64) float64
- func QueryInt(r *http.Request, key string, defaultValue int) int
- func QueryInt32(r *http.Request, key string, defaultValue int32) int32
- func QueryInt64(r *http.Request, key string, defaultValue int64) int64
- func QueryString(r *http.Request, key string, defaultValue string) string
- func QueryStringArray(r *http.Request, key string, defaultValue []string) []string
- func QueryTime(r *http.Request, key, layout string) time.Time
- func QueryTimeWithDefault(r *http.Request, key, layout string, defaultValue time.Time) time.Time
- func ReadJSON(r *http.Request, dst interface{}) error
- func Recover(w http.ResponseWriter, r *http.Request)
- func RecoverJSON(w http.ResponseWriter, r *http.Request)
- func WriteError(w http.ResponseWriter, err interface{})
- func WriteJSON(w http.ResponseWriter, data interface{})
- func WriteJSONCode(w http.ResponseWriter, code int, data interface{})
- func WriteJSONError(w http.ResponseWriter, err interface{})
- type GrpcError
- type InvalidJSONError
- type InvalidMethodError
- type InvalidParameterError
- type InvalidXSRFToken
- type MissingParameterError
- type NotFoundError
- type NotImplementedError
- type ServerError
- type TimeoutError
- type UnauthorizedError
- type UnprocessableEntityError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BadRequestError ¶
func BadRequestError(w http.ResponseWriter, errorMessage string, args ...interface{})
BadRequestError returns HTTP status 400 and an error message as HTML.
func BearerToken ¶
BearerToken extracts the Bearer token from the request.
func DumpRequestOut ¶
DumpRequestOut prints the request to the given io.Writer.
func EqualJSON ¶ added in v1.0.2
EqualJSON compares the two serialized byte slices for equality.
Both a and b are expected to be JSON serialized byte slices. EqualJSON simply ensures that insiginificant white space is removed both from a and b before comparing for equality.
EqualJSON returns true in the following cases:
- a or b are both nil
- a or b have both a length of 0
- a or b are equal if all siginificant white space is removed, i.e. newlines, tabs, and space.
In all other cases, EqualJSON returns false. Notice that while the two JSON objects `{"a":1,"b":2}` and `{"b":2,"a":1}` may be semantically equal, EqualJSON will return false.
func ForbiddenError ¶
func ForbiddenError(w http.ResponseWriter, errorMessage string, args ...interface{})
ForbiddenError returns HTTP status 403 and an error message as HTML.
func FormBool ¶
FormBool checks if the request r has a Form value with the specified key that can be converted to a bool. If is doesn't, it will return defaultValue.
func FormFloat64 ¶
FormFloat64 checks if the request r has a Form value with the specified key that can be converted to a float64. If is doesn't, it will return defaultValue.
func FormInt ¶
FormInt checks if the request r has a Form value with the specified key that can be converted to an int. If is doesn't, it will return defaultValue.
func FormInt32 ¶
FormInt32 checks if the request r has a Form value with the specified key that can be converted to an int32. If is doesn't, it will return defaultValue.
func FormInt64 ¶
FormInt64 checks if the request r has a Form value with the specified key that can be converted to an int64. If is doesn't, it will return defaultValue.
func FormString ¶
FormString checks if the request r has a Form value with the specified key. If is doesn't, it will return defaultValue.
func InternalServerError ¶
func InternalServerError(w http.ResponseWriter, r *http.Request, err interface{})
InternalServerError returns HTTP status 500 and an error message as HTML.
func IsGetOrHead ¶
IsGetOrHead returns true if r is a GET or HEAD request.
func IsWebsocketUpgrade ¶
IsWebsocketUpgrade returns true if this is a WebSocket upgrade.
func IsXHR ¶
IsXHR returns true if r is an XHR request. It inspects the Content-Type header for that.
func MustFormBool ¶
MustFormBool checks if the request r has a Form value with the specified key that can be converted to a bool. If is doesn't, it will panic.
func MustFormFloat64 ¶
MustFormFloat64 checks if the request r has a Form value with the specified key that can be converted to a float64. If is doesn't, it will panic.
func MustFormInt ¶
MustFormInt checks if the request r has a Form value with the specified key that can be converted to an int. If is doesn't, it will panic.
func MustFormInt32 ¶
MustFormInt32 checks if the request r has a Form value with the specified key that can be converted to an int32. If is doesn't, it will panic.
func MustFormInt64 ¶
MustFormInt64 checks if the request r has a Form value with the specified key that can be converted to an int64. If is doesn't, it will panic.
func MustFormString ¶
MustFormString checks if the request r has a Form value with the specified key of type string. If is doesn't, it will panic.
func MustParamsBool ¶
MustParamsBool checks if the request r has a routing component with the specified key that can be converted to a bool. If is doesn't, it will panic.
func MustParamsFloat64 ¶
MustParamsFloat64 checks if the request r has a routing component with the specified key that can be converted to a float64. If is doesn't, it will panic.
func MustParamsInt ¶
MustParamsInt checks if the request r has a routing component with the specified key that can be converted to an int. If is doesn't, it will panic.
func MustParamsInt32 ¶
MustParamsInt32 checks if the request r has a routing component with the specified key that can be converted to an int32. If is doesn't, it will panic.
func MustParamsInt64 ¶
MustParamsInt64 checks if the request r has a routing component with the specified key that can be converted to an int64. If is doesn't, it will panic.
func MustParamsString ¶
MustParamsString checks if the request r has a routing component with the specified key. If is doesn't, it will panic.
func MustQueryBool ¶
MustQueryBool checks if the request r has a query string with the specified key that can be converted to a bool. If is doesn't, it will panic.
func MustQueryDuration ¶ added in v1.0.1
MustQueryDuration checks if the request r has a query string with the specified key that can be converted to a time.Duration. If is doesn't, it will return defaultValue or a zero time.
func MustQueryDurationWithDefault ¶ added in v1.0.1
func MustQueryDurationWithDefault(r *http.Request, key string, defaultValue time.Duration) time.Duration
MustQueryDurationWithDefault checks if the request r has a query string with the specified key that can be converted to a time.Duration. If is doesn't, it will return defaultValue or a zero time.
func MustQueryFloat64 ¶
MustQueryFloat64 checks if the request r has a query string with the specified key that can be converted to a float64. If is doesn't, it will panic.
func MustQueryInt ¶
MustQueryInt checks if the request r has a query string with the specified key that can be converted to an int. If is doesn't, it will panic.
func MustQueryInt32 ¶
MustQueryInt32 checks if the request r has a query string with the specified key that can be converted to an int32. If is doesn't, it will panic.
func MustQueryInt64 ¶
MustQueryInt64 checks if the request r has a query string with the specified key that can be converted to an int64. If is doesn't, it will panic.
func MustQueryString ¶
MustQueryString checks if the request r has a query string with the specified key. If is doesn't, it will panic.
Example ¶
package main import ( "fmt" "io/ioutil" "net/http" "net/http/httptest" "github.com/olivere/httputil" ) func main() { handler := func(w http.ResponseWriter, r *http.Request) { defer httputil.RecoverJSON(w, r) name := httputil.MustQueryString(r, "name") fmt.Fprintf(w, "Hello %s", name) } req := httptest.NewRequest("GET", "http://example.com/hello?name=Oliver", nil) w := httptest.NewRecorder() handler(w, req) resp := w.Result() defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }
Output: Hello Oliver
func MustQueryTime ¶
MustQueryTime checks if the request r has a query string with the specified key that can be converted to a time.Time, based on the given layout format. If is doesn't, it will return defaultValue or a zero time.
func MustQueryTimeWithDefault ¶
func MustQueryTimeWithDefault(r *http.Request, key, layout string, defaultValue time.Time) time.Time
MustQueryTimeWithDefault checks if the request r has a query string with the specified key that can be converted to a time.Time, based on the given layout format. If is doesn't, it will return defaultValue or a zero time.
func MustReadJSON ¶
MustReadJSON is like ReadJSON, but panics on errors.
func ParamsBool ¶ added in v1.0.3
ParamsBool checks if the request r has a routing component with the specified key. If is doesn't, it will return defaultValue.
func ParamsFloat64 ¶
ParamsFloat64 checks if the request r has a routing component with the specified key that can be converted to a float64. If is doesn't, it will return defaultValue.
func ParamsInt ¶
ParamsInt checks if the request r has a routing component with the specified key that can be converted to an int. If is doesn't, it will return defaultValue.
func ParamsInt32 ¶
ParamsInt32 checks if the request r has a routing component with the specified key that can be converted to an int32. If is doesn't, it will return defaultValue.
func ParamsInt64 ¶
ParamsInt64 checks if the request r has a routing component with the specified key that can be converted to an int64. If is doesn't, it will return defaultValue.
func ParamsString ¶
ParamsString checks if the request r has a routing component with the specified key. If is doesn't, it will return defaultValue.
func QueryBool ¶
QueryBool checks if the request r has a query string with the specified key that can be converted to a bool. If is doesn't, it will return defaultValue.
func QueryDuration ¶ added in v1.0.1
QueryDuration checks if the request r has a query string with the specified key that can be converted to a time.Duration. If is doesn't, it will return defaultValue or a zero duration.
func QueryDurationWithDefault ¶ added in v1.0.1
func QueryDurationWithDefault(r *http.Request, key string, defaultValue time.Duration) time.Duration
QueryDurationWithDefault checks if the request r has a query string with the specified key that can be converted to a time.Duration. If is doesn't, it will return defaultValue or a zero duration.
func QueryFloat64 ¶
QueryFloat64 checks if the request r has a query string with the specified key that can be converted to a float64. If is doesn't, it will return defaultValue.
func QueryInt ¶
QueryInt checks if the request r has a query string with the specified key that can be converted to an int. If is doesn't, it will return defaultValue.
func QueryInt32 ¶
QueryInt32 checks if the request r has a query string with the specified key that can be converted to an int32. If is doesn't, it will return defaultValue.
func QueryInt64 ¶
QueryInt64 checks if the request r has a query string with the specified key that can be converted to an int64. If is doesn't, it will return defaultValue.
func QueryString ¶
QueryString checks if the request r has a query string with the specified key. If is doesn't, it will return defaultValue.
func QueryStringArray ¶
QueryStringArray checks if the request r has a query string with the specified key. If is doesn't, it will return defaultValue. Otherwise it'll split the string by a comma and return the resulting array.
func QueryTime ¶
QueryTime checks if the request r has a query string with the specified key that can be converted to a time.Time, based on the given layout format. If is doesn't, it will return defaultValue or a zero time.
func QueryTimeWithDefault ¶
QueryTimeWithDefault checks if the request r has a query string with the specified key that can be converted to a time.Time, based on the given layout format. If is doesn't, it will return defaultValue or a zero time.
func ReadJSON ¶
ReadJSON deserializes the body of the request into dst as JSON. A maximum size of 8 MB of JSON are permitted.
Example ¶
package main import ( "fmt" "io/ioutil" "net/http" "strings" "github.com/olivere/httputil" ) func main() { req := &http.Request{ Body: ioutil.NopCloser(strings.NewReader(`{"name":"Oliver"}`)), } var person struct { Name string `json:"name"` } httputil.MustReadJSON(req, &person) fmt.Println(person.Name) }
Output: Oliver
func Recover ¶
func Recover(w http.ResponseWriter, r *http.Request)
Recover can be used as a deferred func to catch panics in an HTTP handler.
func RecoverJSON ¶
func RecoverJSON(w http.ResponseWriter, r *http.Request)
RecoverJSON can be used as a deferred func to catch panics in an HTTP handler and print a JSON error.
Example:
func Handler(w http.ResponseWriter, r *http.Request) { defer httputil.RecoverJSON(w, r) ... panic(errors.New("kaboom")) }
func WriteError ¶
func WriteError(w http.ResponseWriter, err interface{})
WriteError writes an error message for display in a HTML page.
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, data interface{})
WriteJSON writes data as JSON into w with HTTP status code 200.
Example ¶
package main import ( "fmt" "io/ioutil" "net/http" "net/http/httptest" "github.com/olivere/httputil" ) func main() { handler := func(w http.ResponseWriter, r *http.Request) { person := struct { Name string `json:"name"` }{ Name: "Oliver", } httputil.WriteJSON(w, person) } req := httptest.NewRequest("GET", "http://example.com/", nil) w := httptest.NewRecorder() handler(w, req) resp := w.Result() defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }
Output: { "name": "Oliver" }
func WriteJSONCode ¶
func WriteJSONCode(w http.ResponseWriter, code int, data interface{})
WriteJSONCode writes data as JSON into w and sets the HTTP status code.
Example ¶
package main import ( "fmt" "io/ioutil" "net/http" "net/http/httptest" "github.com/olivere/httputil" ) func main() { handler := func(w http.ResponseWriter, r *http.Request) { person := struct { Name string `json:"name"` }{ Name: "Oliver", } httputil.WriteJSONCode(w, http.StatusCreated, person) } req := httptest.NewRequest("GET", "http://example.com/", nil) w := httptest.NewRecorder() handler(w, req) resp := w.Result() defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) fmt.Printf("Status: %d\n", resp.StatusCode) fmt.Println(string(body)) }
Output: Status: 201 { "name": "Oliver" }
func WriteJSONError ¶
func WriteJSONError(w http.ResponseWriter, err interface{})
WriteJSONError writes error information, serialized in a JSON structure. Example:
{ "error":{ "code": 500, "message": "Something went wrong", "details": ["A was bad", "B is missing"] } }
If err implements the httpCoder interface, it can specify the HTTP code to return. If err implements the httpErrorDetails interface, its ErrorDetails func is used to collect the error details; otherwise, the "details" field is missing in the error returned.
Types ¶
type GrpcError ¶
type GrpcError struct {
Err error
}
GrpcError is a placeholder for a gRPC error, and will turn it into a HTTP error.
type InvalidJSONError ¶
type InvalidJSONError struct {
// contains filtered or unexported fields
}
InvalidJSONError indicates that the JSON data are invalid.
func (InvalidJSONError) HTTPCode ¶
func (InvalidJSONError) HTTPCode() int
HTTPCode returns the HTTP status code of the error.
type InvalidMethodError ¶
type InvalidMethodError struct{}
InvalidMethodError indicates that an invalid HTTP method is being used.
func (InvalidMethodError) Error ¶
func (InvalidMethodError) Error() string
Error returns the error in text form.
func (InvalidMethodError) HTTPCode ¶
func (InvalidMethodError) HTTPCode() int
HTTPCode returns the HTTP status code of the error.
type InvalidParameterError ¶
type InvalidParameterError string
InvalidParameterError indicates that a parameter is invalid.
func (InvalidParameterError) Error ¶
func (p InvalidParameterError) Error() string
Error returns the error in text form.
func (InvalidParameterError) HTTPCode ¶
func (InvalidParameterError) HTTPCode() int
HTTPCode returns the HTTP status code of the error.
type InvalidXSRFToken ¶
type InvalidXSRFToken struct{}
InvalidXSRFToken indicates that the user has not provided a valid XSRF token.
func (InvalidXSRFToken) Error ¶
func (InvalidXSRFToken) Error() string
Error returns the error in text form.
func (InvalidXSRFToken) HTTPCode ¶
func (InvalidXSRFToken) HTTPCode() int
HTTPCode returns the HTTP status code of the error.
type MissingParameterError ¶
type MissingParameterError string
MissingParameterError indicates that a required parameter is missing or blank.
Example ¶
package main import ( "fmt" "io/ioutil" "net/http" "net/http/httptest" "github.com/olivere/httputil" ) func main() { handler := func(w http.ResponseWriter, r *http.Request) { defer httputil.RecoverJSON(w, r) name := httputil.QueryString(r, "name", "") if name == "" { panic(httputil.MissingParameterError("name")) } httputil.WriteJSONCode(w, http.StatusOK, struct { Message string `json:"name"` }{ Message: fmt.Sprintf("Hello %s", name), }) } req := httptest.NewRequest("GET", "http://example.com/hello?name=", nil) w := httptest.NewRecorder() handler(w, req) resp := w.Result() defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) fmt.Printf("Status: %d\n", resp.StatusCode) fmt.Println(string(body)) }
Output: Status: 400 { "error": { "code": 400, "message": "Missing parameter \"name\"" } }
func (MissingParameterError) Error ¶
func (p MissingParameterError) Error() string
Error returns the error in text form.
func (MissingParameterError) HTTPCode ¶
func (MissingParameterError) HTTPCode() int
HTTPCode returns the HTTP status code of the error.
type NotFoundError ¶
type NotFoundError struct{}
NotFoundError indicates that a record or resource does not exist.
func (NotFoundError) Error ¶
func (NotFoundError) Error() string
Error returns the error in text form.
func (NotFoundError) HTTPCode ¶
func (NotFoundError) HTTPCode() int
HTTPCode returns the HTTP status code of the error.
type NotImplementedError ¶
type NotImplementedError struct{}
NotImplementedError indicates that an endpoint has yet to be implemented.
func (NotImplementedError) Error ¶
func (NotImplementedError) Error() string
Error returns the error in text form.
func (NotImplementedError) HTTPCode ¶
func (NotImplementedError) HTTPCode() int
HTTPCode returns the HTTP status code of the error.
type ServerError ¶
type ServerError string
ServerError indicates any kind of internal server problem.
func (ServerError) Error ¶
func (e ServerError) Error() string
Error returns the error in text form.
func (ServerError) HTTPCode ¶
func (ServerError) HTTPCode() int
HTTPCode returns the HTTP status code of the error.
type TimeoutError ¶
type TimeoutError struct{}
TimeoutError indicates that the request has timed out.
func (TimeoutError) Error ¶
func (TimeoutError) Error() string
Error returns the error in text form.
func (TimeoutError) HTTPCode ¶
func (TimeoutError) HTTPCode() int
HTTPCode returns the HTTP status code of the error.
type UnauthorizedError ¶
type UnauthorizedError struct{}
UnauthorizedError indicates that credentials are either missing or invalid.
func (UnauthorizedError) Error ¶
func (UnauthorizedError) Error() string
Error returns the error in text form.
func (UnauthorizedError) HTTPCode ¶
func (UnauthorizedError) HTTPCode() int
HTTPCode returns the HTTP status code of the error.
type UnprocessableEntityError ¶
type UnprocessableEntityError struct {
Errors []string
}
UnprocessableEntityError indicates that there was a semantic error in parsing a request, e.g. a record with validation errors.
func (UnprocessableEntityError) Error ¶
func (UnprocessableEntityError) Error() string
Error returns the error in text form.
func (UnprocessableEntityError) ErrorDetails ¶
func (p UnprocessableEntityError) ErrorDetails() []string
ErrorDetails returns additional information about the error.
func (UnprocessableEntityError) HTTPCode ¶
func (UnprocessableEntityError) HTTPCode() int
HTTPCode returns the HTTP status code of the error.