Documentation
¶
Overview ¶
Package response provides HTTP response and error helpers that negotiate between HTML and JSON based on the request's Accept and Content-Type headers.
The error helpers (InternalError, Unauthorized, BadRequest, NotFound, MissingSession) delegate to a github.com/mikehelmick/go-bananas/render.Renderer to render the appropriate "401"/"404"/"500" template for HTML clients or a `{"error":...}` body for JSON clients, falling back to plain text otherwise.
Index ¶
- Constants
- func Back(w http.ResponseWriter, r *http.Request, _ *render.Renderer)
- func BadRequest(w http.ResponseWriter, r *http.Request, h *render.Renderer)
- func InternalError(w http.ResponseWriter, r *http.Request, h *render.Renderer, err error)
- func IsJSONContentType(r *http.Request) bool
- func MissingSession(w http.ResponseWriter, r *http.Request, h *render.Renderer)
- func NotFound(w http.ResponseWriter, r *http.Request, h *render.Renderer)
- func RealHostFromRequest(r *http.Request) string
- func SeeOther(w http.ResponseWriter, r *http.Request, path string)
- func TracedHTTPClient(timeout time.Duration) *http.Client
- func Unauthorized(w http.ResponseWriter, r *http.Request, h *render.Renderer)
Constants ¶
const ( // ContentTypeJSON is the canonical JSON media type. ContentTypeJSON = "application/json" // ContentTypeHTML is the canonical HTML media type. ContentTypeHTML = "text/html" )
Variables ¶
This section is empty.
Functions ¶
func Back ¶
Back redirects to the request's referrer with a 303 See Other. If the referrer is missing, unparseable, or points at a different host, it redirects to "/" instead, preventing open-redirect abuse. The renderer parameter is accepted for signature consistency with the other helpers but is unused.
func BadRequest ¶
BadRequest renders a 400 response negotiated for the client.
func InternalError ¶
InternalError logs err and renders a 500 response negotiated for the client (HTML, JSON, or plain text).
func IsJSONContentType ¶
IsJSONContentType reports whether the request's Content-Type is application/json. The comparison is case-insensitive (per RFC 2045) and extra parameters, such as a charset, are allowed.
func MissingSession ¶
MissingSession returns an internal error indicating no session was present on the request context. It usually signals that the session middleware was not installed before the handler that needs it.
func RealHostFromRequest ¶
RealHostFromRequest finds the "best" host for the request. The host may be in the request URL (when absolute) or in the Host header; when developing locally it may be missing entirely. The scheme defaults to http for localhost and https otherwise, and default ports are stripped.
func SeeOther ¶
func SeeOther(w http.ResponseWriter, r *http.Request, path string)
SeeOther issues a 303 See Other redirect to path. It is a convenience for callers that need an http.HandlerFunc-style redirect, such as the onIdle handler of the session-idle middleware.
func TracedHTTPClient ¶
TracedHTTPClient returns an HTTP client whose requests carry the X-Request-ID and X-Cloud-Trace-Context headers from the request context, to correlate service-to-service calls. It should be used for internal calls, not for upstream third-party APIs.
func Unauthorized ¶
Unauthorized renders a 401 response negotiated for the client. The framework always returns 401 (even when authentication is present but authorization fails).
Types ¶
This section is empty.