response

package
v3.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2025 License: MIT Imports: 4 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ContentTypeHeader = "Content-Type"
	ContentTypeJSON   = "application/json"
)

ContentType header possible values

View Source
const (
	ETagHeader = "ETag"
)

Variables

This section is empty.

Functions

func GenerateETag

func GenerateETag(body []byte, weak bool) (etag string)

GenerateETag generates a SHA-1 hash of the body with type []byte. SHA-1 is not cryptographically safe, but it has been selected for performance as we are only interested in uniqueness. A strong or weak eTag can be generated. Please note that ETags are surrounded by double quotes.

Example: ETag = `"24decf55038de874bc6fa9cf0930adc219f15db1"`

The definition of ETag is explained in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag

func SetETag

func SetETag(w http.ResponseWriter, newETag string)

SetETag sets the new eTag of the resource in the header. Please note that ETags are surrounded by double quotes.

Example: ETag = `"24decf55038de874bc6fa9cf0930adc219f15db1"`

func WriteJSON

func WriteJSON(w http.ResponseWriter, value interface{}, status int) error

WriteJSON set the content type header to JSON, writes the response object as json and sets the http status code.

Types

type JSONEncoder

type JSONEncoder interface {
	WriteResponseJSON(w http.ResponseWriter, value interface{}, status int) error
}

JSONEncoder interface defining a JSON encoder.

var JsonResponseEncoder JSONEncoder = &OnsJSONEncoder{}

type OnsJSONEncoder

type OnsJSONEncoder struct{}

OnsJSONEncoder is a JSON encoder

func (*OnsJSONEncoder) WriteResponseJSON

func (j *OnsJSONEncoder) WriteResponseJSON(w http.ResponseWriter, value interface{}, status int) error

WriteResponseJSON marshals the provided value as json body, and sets the status code to the provided value

Jump to

Keyboard shortcuts

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