httputils

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2016 License: GPL-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const APIVersionKey key = 0

APIVersionKey is the client's requested API version.

View Source
const UserKey key = 1

UseKey is the key for userdb.User values in Contexts.

Variables

This section is empty.

Functions

func CheckForJSON

func CheckForJSON(r *http.Request) error

CheckForJSON makes sure that the request's Content-Type is application/json.

func GetHTTPErrorStatusCode

func GetHTTPErrorStatusCode(err error) int

GetHTTPErrorStatusCode retrieve status code from error message

func MatchesContentType

func MatchesContentType(contentType, expectedType string) bool

MatchesContentType validates the content type against the expected one

func NewStatusError

func NewStatusError(code int) error

NewStatusError returns a error object with HTTP status code.

func ParseForm

func ParseForm(r *http.Request) error

ParseForm ensures the request form is parsed even with invalid content types. If we don't do this, POST method without Content-type (even with empty body) will fail.

func UserFromContext

func UserFromContext(ctx context.Context) (user *userdb.BasicUser)

UserFromContext returns the authenticated user from the context using UserKey.

func VersionFromContext

func VersionFromContext(ctx context.Context) (ver string)

VersionFromContext returns an API version from the context using APIVersionKey.

func WriteError

func WriteError(w http.ResponseWriter, r *http.Request, err error)

WriteError decodes a specific error and sends it in the response.

func WriteJSON

func WriteJSON(w http.ResponseWriter, code int, v interface{}) error

WriteJSON writes the value to the http response stream as json with standard json encoding.

Types

type APIFunc

type APIFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error

APIFunc is an adapter to allow the use of ordinary functions as API endpoints. Any function that has the appropriate signature can be registered as a API endpoint.

Jump to

Keyboard shortcuts

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