Documentation ¶
Overview ¶
Package response provides encapsulated HTTP responses.
Index ¶
- type Builder
- func (builder *Builder) Body() body.Body
- func (builder *Builder) Build() *Response
- func (builder *Builder) Copy() (*Builder, error)
- func (builder *Builder) Err() error
- func (builder *Builder) ErrorPage(code int, err error) *Builder
- func (builder *Builder) Headers() http.Header
- func (builder *Builder) PageGenerator() PageGenerator
- func (builder *Builder) RedirectPage(code int, location string) *Builder
- func (builder *Builder) Status() int
- func (builder *Builder) WithBody(b body.Body) *Builder
- func (builder *Builder) WithContentEncoding(str string) *Builder
- func (builder *Builder) WithContentLanguage(str string) *Builder
- func (builder *Builder) WithContentType(str string) *Builder
- func (builder *Builder) WithDigest(algo string, sum []byte) *Builder
- func (builder *Builder) WithETag(tag string, isStrong bool) *Builder
- func (builder *Builder) WithError(err error) *Builder
- func (builder *Builder) WithHeader(name string, value string, appendToExisting bool) *Builder
- func (builder *Builder) WithHeaders(hdrs http.Header) *Builder
- func (builder *Builder) WithJSON(v interface{}) *Builder
- func (builder *Builder) WithPageGenerator(gen PageGenerator) *Builder
- func (builder *Builder) WithPrettyJSON(v interface{}) *Builder
- func (builder *Builder) WithProto(msg proto.Message) *Builder
- func (builder *Builder) WithProtoText(msg proto.Message, o *prototext.MarshalOptions) *Builder
- func (builder *Builder) WithStatus(code int) *Builder
- func (builder *Builder) WithoutHeader(name string) *Builder
- type PageGenerator
- type Response
- type Writer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
func NewBuilder ¶
func NewBuilder() *Builder
func (*Builder) Body ¶
Body returns the associated HTTP response body.
The caller must not read from or modify the returned Body. Call Body.Copy() and operate on the copy when performing such actions.
func (*Builder) Build ¶
Build returns a new Response with the properties specified on this Builder.
A Body MUST already have been specified. The Response takes ownership of the Body.
If the Content-Type header has not been specified, then the Content-Type header is automatically populated as "application/octet-stream".
If the Content-Length header has not been specified AND the Body has a non-negative BytesRemaining(), then the Content-Length header is automatically populated from BytesRemaining().
After calling this method, the Builder is reset to an empty state and is ready to build another Response. Only the PageGenerator is retained.
func (*Builder) Headers ¶
Headers returns the associated HTTP response headers.
The caller is allowed to mutate the returned map and its contents.
func (*Builder) PageGenerator ¶
func (builder *Builder) PageGenerator() PageGenerator
PageGenerator returns the associated PageGenerator instance, or DefaultPageGenerator if not set.
func (*Builder) RedirectPage ¶
RedirectPage populates this Builder with a generated redirect response.
func (*Builder) WithBody ¶
WithBody associates the given Body with this Builder, taking ownership of it.
The given value MUST NOT be nil.
func (*Builder) WithContentEncoding ¶
WithContentEncoding sets the Content-Encoding header.
func (*Builder) WithContentLanguage ¶
WithContentLanguage sets the Content-Language header.
func (*Builder) WithContentType ¶
WithContentType sets the Content-Type header.
func (*Builder) WithDigest ¶
WithDigest adds the given Digest header.
func (*Builder) WithError ¶
WithError associates the given Go error with this Builder.
The given value MAY be nil.
func (*Builder) WithHeader ¶
WithHeader adds the given header to the HTTP response.
func (*Builder) WithHeaders ¶
WithHeaders associates the given HTTP response headers with this Builder.
This method makes a deep copy of the given map and its contents, so that no references to it are retained.
func (*Builder) WithJSON ¶
WithJSON converts the given value to a JSON Body, then associates it with this Builder.
This method also adds the header "Content-Type: application/json".
func (*Builder) WithPageGenerator ¶
func (builder *Builder) WithPageGenerator(gen PageGenerator) *Builder
WithPageGenerator associates the given PageGenerator instance with this Builder. This affects future calls to RedirectPage or ErrorPage.
The given value MUST NOT be nil.
func (*Builder) WithPrettyJSON ¶
WithPrettyJSON converts the given value to a human-formatted JSON Body, then associates it with this Builder.
This method also adds the header "Content-Type: application/json".
func (*Builder) WithProto ¶
WithProto converts the given value to a binary protobuf Body, then associates it with this Builder.
This method also adds the header "Content-Type: application/vnd.google.protobuf".
func (*Builder) WithProtoText ¶
WithProtoText converts the given value to a text protobuf Body, then associates it with this Builder.
This method also adds the header "Content-Type: text/plain; charset=utf-8".
func (*Builder) WithStatus ¶
WithStatus associates the given HTTP status code with this Builder.
The given value MUST lie between 200 and 999 inclusive.
func (*Builder) WithoutHeader ¶
WithoutHeader removes the given header from the HTTP response.
type PageGenerator ¶
type PageGenerator interface { GenerateRedirectPage(code int, location string) (http.Header, body.Body) GenerateErrorPage(code int, err error) (http.Header, body.Body) }
var DefaultPageGenerator PageGenerator = &defaultPageGenerator{}
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response represents an HTTP response.
func (*Response) Body ¶
Body returns this Response's Body.
The caller must not read from or modify the returned Body. Call Body.Copy() and operate on the copy when performing such actions.
func (*Response) Headers ¶
Headers returns the HTTP headers of the response.
The caller MUST NOT modify the returned map or its contents.
func (*Response) Serve ¶
func (resp *Response) Serve(w http.ResponseWriter) error
Serve serves the Response via the given ResponseWriter, consuming its Body.