httpresponse

package
v1.4.15 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: Apache-2.0 Imports: 9 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ContentTypeKey        = "Content-Type"
	ContentLengthKey      = "Content-Length"
	ContentTypeJSON       = "application/json"
	ContentTypeText       = "text/plain"
	ContentTypeTextStream = "text/event-stream"
)

Variables

This section is empty.

Functions

func Cors added in v1.4.11

func Cors(w http.ResponseWriter, origin string, methods ...string) error

Cors is a utility function to set the CORS headers on a pre-flight request. Setting origin to an empty string or not including methods will allow any origin and any method

func Empty

func Empty(w http.ResponseWriter, code int, tuples ...string) error

Empty is a utility function to serve an empty response. Additional header tuples can be provided as a series of key-value pairs

func Error

func Error(w http.ResponseWriter, code int, reason ...string) error

Error is a utility function to serve a JSON error notice

func ErrorWith added in v1.4.11

func ErrorWith(w http.ResponseWriter, code int, v any, reason ...string) error

ErrorWith is a utility function to serve a JSON error notice with additional details

func JSON

func JSON(w http.ResponseWriter, v interface{}, code int, indent uint, tuples ...string) error

JSON is a utility function to serve an arbitary object as JSON. Setting the indent value to a non-zero value will indent the JSON. Additional header tuples can be provided as a series of key-value pairs

func Text

func Text(w http.ResponseWriter, v string, code int, tuples ...string) error

Text is a utility function to serve plaintext. Additional header tuples can be provided as a series of key-value pairs

Types

type ErrorResponse

type ErrorResponse struct {
	Code   int    `json:"code"`
	Reason string `json:"reason,omitempty"`
	Detail any    `json:"detail,omitempty"`
}

ErrorResponse is a generic error response which is served as JSON using the ServeError method

type TextStream added in v1.4.14

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

TextStream implements a stream of text events

func NewTextStream added in v1.4.14

func NewTextStream(w http.ResponseWriter, tuples ...string) *TextStream

Create a new text stream with mimetype text/event-stream Additional header tuples can be provided as a series of key-value pairs

func (*TextStream) Close added in v1.4.14

func (s *TextStream) Close() error

Close the text stream to stop sending ping messages

func (*TextStream) Write added in v1.4.14

func (s *TextStream) Write(name string, data ...any)

Write a text event to the stream, and one or more optional data objects which are encoded as JSON

Jump to

Keyboard shortcuts

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