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) while still exposing straightforward HTTP semantics.
Package response intentionally avoids hidden policy and keeps behavior predictable so higher-level runtime layers can compose it safely.
Index ¶
- Constants
- func GetClientRedirectURL(w http.ResponseWriter) string
- type Proxy
- func (p *Proxy) AddHeadEls(els *headels.HeadEls)
- func (p *Proxy) AddHeader(key, value string)
- func (p *Proxy) ApplyToResponseWriter(w http.ResponseWriter, r *http.Request)
- func (p *Proxy) Cookies() []*http.Cookie
- func (p *Proxy) HeadEls() *headels.HeadEls
- func (p *Proxy) HeadElsIfPresent() *headels.HeadEls
- func (p *Proxy) Header(key string) string
- func (p *Proxy) Headers(key string) []string
- func (p *Proxy) IsError() bool
- func (p *Proxy) IsRedirect() bool
- func (p *Proxy) IsSuccess() bool
- func (p *Proxy) Location() string
- func (p *Proxy) Redirect(r *http.Request, url string, code ...int) (bool, error)
- func (p *Proxy) SetCookie(cookie *http.Cookie)
- func (p *Proxy) SetHeader(key, value string)
- func (p *Proxy) SetStatus(status int, errorStatusText ...string)
- func (p *Proxy) Status() (int, string)
- type Response
- func (res *Response) AddHeader(key, value string)
- func (res *Response) BadRequest(reasons ...string)
- func (res *Response) ClientRedirect(url string) error
- func (res *Response) Error(status int, reasons ...string)
- func (res *Response) Forbidden(reasons ...string)
- func (res *Response) HTML(html string)
- func (res *Response) HTMLBytes(bytes []byte)
- func (res *Response) InternalServerError(reasons ...string)
- func (res *Response) IsCommitted() bool
- func (res *Response) JSON(v any)
- func (res *Response) JSONBytes(bytes []byte)
- func (res *Response) MethodNotAllowed(reasons ...string)
- func (res *Response) NotFound()
- func (res *Response) NotModified()
- func (res *Response) OK()
- func (res *Response) OKText()
- func (res *Response) Redirect(r *http.Request, url string, code ...int) (usedClientRedirect bool, err error)
- func (res *Response) ServerRedirect(r *http.Request, url string, code ...int)
- func (res *Response) SetHeader(key, value string)
- func (res *Response) SetStatus(status int)
- func (res *Response) Text(text string)
- func (res *Response) TooManyRequests(reasons ...string)
- func (res *Response) Unauthorized(reasons ...string)
Constants ¶
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
}
For usage in JSON API handlers that may run in parallel or do not have direct access to the http.ResponseWriter. Proxy instances are not meant to be shared. Rather, they should exist inside a single function/handler scope, and afterwards should be used by a parent scope to actually de-duplicate, determine priority, and write to the real http.ResponseWriter.
Concurrency model: Proxy instances are NOT thread-safe and must not be shared across goroutines. The intended usage pattern is:
- Create one Proxy per goroutine/task
- Each goroutine writes only to its own Proxy
- After all goroutines complete, merge proxies on a single goroutine using MergeProxyResponses
- Apply the merged result to the ResponseWriter
Do not instantiate directly. Use NewProxy().
func MergeProxyResponses ¶
Consumers should deduplicate head els after calling MergeProxyResponses by using headels.ToHeadEls(proxy.HeadEls())
func (*Proxy) AddHeadEls ¶ added in v0.83.0
func (*Proxy) ApplyToResponseWriter ¶
func (p *Proxy) ApplyToResponseWriter(w http.ResponseWriter, r *http.Request)
func (*Proxy) HeadElsIfPresent ¶
HeadElsIfPresent returns the proxy head elements only when already present.
func (*Proxy) IsRedirect ¶
type Response ¶
type Response struct {
Writer http.ResponseWriter
// contains filtered or unexported fields
}
Response is a small convenience wrapper for writing HTTP responses.
func (*Response) AddHeader ¶
AddHeader appends a response header value without committing the response.
func (*Response) BadRequest ¶
BadRequest writes a 400 response.
func (*Response) ClientRedirect ¶
ClientRedirect sets a client-side redirect header and status 200. Returns an error if the response is already committed.
func (*Response) InternalServerError ¶
InternalServerError writes a 500 response.
func (*Response) IsCommitted ¶
IsCommitted reports whether this helper has already written a status or body.
func (*Response) MethodNotAllowed ¶
MethodNotAllowed writes a 405 response.
func (*Response) Redirect ¶
func (res *Response) Redirect( r *http.Request, url string, code ...int, ) (usedClientRedirect bool, err error)
Redirect chooses client redirect headers or server redirects based on request headers.
func (*Response) ServerRedirect ¶
ServerRedirect writes an HTTP redirect response.
func (*Response) TooManyRequests ¶
TooManyRequests writes a 429 response.
func (*Response) Unauthorized ¶
Unauthorized writes a 401 response.