response

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

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

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

func BadRequest(w http.ResponseWriter, r *http.Request, h *render.Renderer)

BadRequest renders a 400 response negotiated for the client.

func InternalError

func InternalError(w http.ResponseWriter, r *http.Request, h *render.Renderer, err error)

InternalError logs err and renders a 500 response negotiated for the client (HTML, JSON, or plain text).

func IsJSONContentType

func IsJSONContentType(r *http.Request) bool

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

func MissingSession(w http.ResponseWriter, r *http.Request, h *render.Renderer)

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 NotFound

func NotFound(w http.ResponseWriter, r *http.Request, h *render.Renderer)

NotFound renders a 404 response negotiated for the client.

func RealHostFromRequest

func RealHostFromRequest(r *http.Request) string

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

func TracedHTTPClient(timeout time.Duration) *http.Client

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

func Unauthorized(w http.ResponseWriter, r *http.Request, h *render.Renderer)

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.

Jump to

Keyboard shortcuts

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