response

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNonFlushableWriter = errors.New("non-flushable response writer")

ErrNonFlushableWriter is returned when the http.ResponseWriter does not implement the http.Flusher interface, which is required for streaming responses.

Functions

func Bytes

func Bytes(w http.ResponseWriter, status int, data []byte) error

Bytes writes binary data to the response writer with the appropriate Content-Type header for binary content (application/octet-stream). This is useful for serving files, images, or any binary data that should be treated as a download or binary stream.

Parameters:

  • w: The HTTP response writer
  • status: The HTTP status code to set
  • data: The binary data to write to the response

func HTML

func HTML(w http.ResponseWriter, status int, data string) error

HTML writes HTML content to the response writer with the appropriate Content-Type header for HTML (text/html). This is used for serving static HTML content or HTML strings that have been pre-generated.

Parameters:

  • w: The HTTP response writer
  • status: The HTTP status code to set
  • data: The HTML string to write to the response

func HTMLTemplate

func HTMLTemplate(w http.ResponseWriter, status int, tmpl template.Template, data any) error

HTMLTemplate executes an HTML template with the provided data and writes the result as HTML to the response writer. The Content-Type is set to text/html. This is the standard way to serve dynamic HTML pages in web applications using Go's html/template package.

Parameters:

  • w: The HTTP response writer
  • status: The HTTP status code to set (note: this is set after template execution)
  • tmpl: The HTML template to execute
  • data: The data to pass to the template for execution

func JSON

func JSON[T any](w http.ResponseWriter, status int, data T) error

JSON serializes the given data to JSON format and writes it to the response writer. It automatically sets the Content-Type header to application/json and uses Go's json.Encoder for efficient streaming serialization. This is the standard way to serve JSON API responses.

The function uses generics to provide type safety for the input data while still accepting any serializable type.

Parameters:

  • w: The HTTP response writer
  • status: The HTTP status code to set
  • data: The data to serialize as JSON (must be JSON-serializable)

func Raw

func Raw(w http.ResponseWriter, status int, data []byte) error

Raw writes raw byte data to the response writer with the specified status code. It does not set any Content-Type header, allowing the caller full control over the response format. This is the most basic response function that other response functions build upon.

Parameters:

  • w: The HTTP response writer
  • status: The HTTP status code to set
  • data: The raw byte data to write to the response

func Redirect added in v0.1.1

func Redirect(w http.ResponseWriter, status int, url string) error

Redirect sends an HTTP redirect response to the specified URL with the given status code. This is a generic redirect function that allows you to specify any redirect status code. The Location header is set to the provided URL and the appropriate status code is returned.

Common redirect status codes:

  • 301: Moved Permanently
  • 302: Found (temporary redirect)
  • 303: See Other
  • 307: Temporary Redirect
  • 308: Permanent Redirect

Parameters:

  • w: The HTTP response writer
  • status: The HTTP redirect status code to set
  • url: The URL to redirect the user to

func SSE

func SSE(w http.ResponseWriter, r *http.Request, c <-chan []byte) error

SSE (Server-Sent Events) sends data from a channel to an HTTP client using the Server-Sent Events protocol. It sets the appropriate Content-Type header for SSE and delegates to the Stream function for the actual streaming logic. This is useful for implementing real-time updates to web browsers that support the EventSource API.

Parameters:

  • w: The HTTP response writer
  • r: The HTTP request (used for context cancellation)
  • c: A receive-only channel providing byte slices to stream as SSE data

func Status added in v0.1.1

func Status(w http.ResponseWriter, status int) error

Status sets the HTTP status code for the response without writing any body content. This is useful for responses that only need to communicate a status (like 204 No Content, 201 Created, or various error codes) without additional data. The response body will remain empty after calling this function.

Parameters:

  • w: The HTTP response writer
  • status: The HTTP status code to set

func Stream

func Stream(w http.ResponseWriter, r *http.Request, c <-chan []byte) error

Stream sends data from a channel to an HTTP client in real-time using HTTP streaming. It sets appropriate headers for streaming and flushes data as it becomes available. The function returns when the channel is closed (graceful shutdown) or when the request context is canceled. If the ResponseWriter doesn't support flushing, it returns ErrNonFlushableWriter.

Parameters:

  • w: The HTTP response writer
  • r: The HTTP request (used for context cancellation)
  • c: A receive-only channel providing byte slices to stream

func String

func String(w http.ResponseWriter, status int, data string) error

String writes plain text data to the response writer with the appropriate Content-Type header for text content (text/plain; charset=utf-8). This is ideal for serving plain text responses, logs, or simple text data.

Parameters:

  • w: The HTTP response writer
  • status: The HTTP status code to set
  • data: The string data to write to the response

func StringTemplate

func StringTemplate(w http.ResponseWriter, status int, tmpl template.Template, data any) error

StringTemplate executes a text template with the provided data and writes the result as plain text to the response writer. The Content-Type is set to text/plain; charset=utf-8. This is useful for generating plain text content from templates, such as emails or configuration files.

Parameters:

  • w: The HTTP response writer
  • status: The HTTP status code to set (note: this is set after template execution)
  • tmpl: The text template to execute
  • data: The data to pass to the template for execution

func XML

func XML(w http.ResponseWriter, status int, data any) error

XML serializes the given data to XML format and writes it to the response writer. It automatically sets the Content-Type header to application/xml and uses Go's xml.Encoder for streaming serialization. This is useful for serving XML API responses or RSS feeds.

The data should be a struct with appropriate xml tags for proper marshaling, or implement xml.Marshaler interface for custom serialization.

Parameters:

  • w: The HTTP response writer
  • status: The HTTP status code to set
  • data: The data to serialize as XML (must be XML-serializable)

Types

This section is empty.

Jump to

Keyboard shortcuts

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