response

package
v0.85.0-pre.6 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package response provides thin, explicit HTTP response helpers.

The Response type wraps http.ResponseWriter and centralizes common response patterns (JSON, text, HTML, redirects, status/error helpers).

The Proxy type buffers response intent (headers, cookies, status, redirects) for handlers that run in parallel or lack direct ResponseWriter access. Proxy instances are NOT thread-safe — create one per goroutine/task, then merge on a single goroutine via MergeProxyResponses before applying.

Index

Constants

View Source
const (
	// ClientRedirectHeader carries a client-consumed redirect target URL.
	ClientRedirectHeader = "X-Client-Redirect"
	// ClientAcceptsRedirectHeader opts-in to client redirect responses.
	ClientAcceptsRedirectHeader = "X-Accepts-Client-Redirect"
)

Variables

This section is empty.

Functions

func GetClientRedirectURL

func GetClientRedirectURL(w http.ResponseWriter) string

GetClientRedirectURL reads the client redirect target header from w.

Types

type Proxy

type Proxy struct {
	// contains filtered or unexported fields
}

Proxy buffers response intent for deferred or parallel handlers. Do not instantiate directly — use NewProxy().

func MergeProxyResponses

func MergeProxyResponses(proxies ...*Proxy) *Proxy

MergeProxyResponses merges multiple proxies into one. Rules:

  • Head elements, headers, cookies: merged in order (later cookies dedupe by name).
  • Status: first error wins, otherwise last success wins.
  • Redirect: first redirect wins (assuming no error).

func NewProxy

func NewProxy() *Proxy

NewProxy creates an empty Proxy.

func (*Proxy) AddHeadEls added in v0.83.0

func (p *Proxy) AddHeadEls(els *headels.HeadEls)

AddHeadEls merges head elements into the proxy.

func (*Proxy) AddHeader

func (p *Proxy) AddHeader(key, value string)

AddHeader appends a header value for key.

func (*Proxy) ApplyToResponseWriter

func (p *Proxy) ApplyToResponseWriter(w http.ResponseWriter, r *http.Request)

ApplyToResponseWriter writes all buffered proxy state to the real writer.

func (*Proxy) Cookies

func (p *Proxy) Cookies() []*http.Cookie

Cookies returns all cookies set on the proxy.

func (*Proxy) HeadEls

func (p *Proxy) HeadEls() *headels.HeadEls

HeadEls returns the proxy's head elements, creating them if needed.

func (*Proxy) Header

func (p *Proxy) Header(key string) string

Header returns the first value for key, or "".

func (*Proxy) Headers

func (p *Proxy) Headers(key string) []string

Headers returns all values for key.

func (*Proxy) IsError

func (p *Proxy) IsError() bool

IsError reports whether the status is 400+.

func (*Proxy) IsRedirect

func (p *Proxy) IsRedirect() bool

IsRedirect reports whether any redirect (server or client) is set.

func (*Proxy) IsSuccess

func (p *Proxy) IsSuccess() bool

IsSuccess reports whether the status is 2xx.

func (*Proxy) Location

func (p *Proxy) Location() string

Location returns the redirect URL, if set.

func (*Proxy) Redirect

func (p *Proxy) Redirect(
	r *http.Request, url string, code ...int,
) (bool, error)

Redirect sets a redirect on the proxy. Returns whether a client redirect was used and any error from URL validation.

func (*Proxy) SetCookie

func (p *Proxy) SetCookie(cookie *http.Cookie)

SetCookie appends a cookie to the proxy.

func (*Proxy) SetHeader

func (p *Proxy) SetHeader(key, value string)

SetHeader replaces header values for key.

func (*Proxy) SetStatus

func (p *Proxy) SetStatus(status int, error_text ...string)

SetStatus sets the response status and optional error text.

func (*Proxy) Status

func (p *Proxy) Status() (int, string)

Status returns the status code and error text.

type Response

type Response struct {
	Writer http.ResponseWriter
	// contains filtered or unexported fields
}

Response is a convenience wrapper for writing HTTP responses.

func New

New creates a Response helper around w.

func (*Response) AddHeader

func (res *Response) AddHeader(key, value string)

AddHeader appends a response header value without committing the response.

func (*Response) BadRequest

func (res *Response) BadRequest(
	reasons ...string,
)

func (*Response) ClientRedirect

func (res *Response) ClientRedirect(url string) error

ClientRedirect sets a client-side redirect header and status 200.

func (*Response) Error

func (res *Response) Error(status int, reasons ...string)

Error writes an HTTP error with optional custom reason text.

func (*Response) Forbidden

func (res *Response) Forbidden(
	reasons ...string,
)

func (*Response) HTML

func (res *Response) HTML(html string)

HTML writes an HTML string.

func (*Response) HTMLBytes

func (res *Response) HTMLBytes(bytes []byte)

HTMLBytes writes HTML bytes.

func (*Response) InternalServerError

func (res *Response) InternalServerError(reasons ...string)

func (*Response) IsCommitted

func (res *Response) IsCommitted() bool

IsCommitted reports whether a status or body has already been written.

func (*Response) JSON

func (res *Response) JSON(v any)

JSON marshals v as JSON and writes it.

func (*Response) JSONBytes

func (res *Response) JSONBytes(bytes []byte)

JSONBytes writes pre-encoded JSON bytes.

func (*Response) MethodNotAllowed

func (res *Response) MethodNotAllowed(reasons ...string)

func (*Response) NotFound

func (res *Response) NotFound()

func (*Response) NotModified

func (res *Response) NotModified()

func (*Response) OK

func (res *Response) OK()

OK writes a 200 response with {"ok":true}.

func (*Response) OKText

func (res *Response) OKText()

OKText writes a 200 response with "OK".

func (*Response) Redirect

func (res *Response) Redirect(
	r *http.Request, url string, code ...int,
) (used_client_redirect bool, err error)

Redirect chooses client or server redirect based on request headers.

func (*Response) ServerRedirect

func (res *Response) ServerRedirect(r *http.Request, url string, code ...int)

ServerRedirect writes an HTTP redirect response.

func (*Response) SetHeader

func (res *Response) SetHeader(key, value string)

SetHeader sets a response header without committing the response.

func (*Response) SetStatus

func (res *Response) SetStatus(status int)

SetStatus writes the status code and marks the response committed.

func (*Response) Text

func (res *Response) Text(text string)

Text writes plain text.

func (*Response) TooManyRequests

func (res *Response) TooManyRequests(reasons ...string)

func (*Response) Unauthorized

func (res *Response) Unauthorized(
	reasons ...string,
)

Jump to

Keyboard shortcuts

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