🧱More into responses

Page description (optional)

Responses were already briefly explained before. Here's more detailed intercourse.

Fun fact: there's a shortcut for request.Respond() directly: http.Respond. It's primarily intended to be used as a dummy handler always returning 200 OK:

r.Get("/", http.Respond)

Response methods

Code(status.Code) *http.Response

Sets the response code. By default is set to 200 OK.

Status(status.Status) *http.Response

Sets the response status text. Is automatically chosen based on the response code. For all non-standard ones, it is Nonstandard .

ContentType(ct mime.MIME, charset ...mime.Charset) *http.Response

Sets the Content-Type value. mime.MIME is a typealias to string. If more than one charset is provided, only the first one is used.

Compression(token string) *http.Response

Forces a specific compressor to be used. If the passed token is unrecognized, the operation is a no-op. So are "" and "identity" tokens.

Compress() *http.Response

Tells the core that the response is wished to be compressed. More about automatic compression in Basic usage.

Header(key string, value ...string) *http.Response

Adds a header to the response. If the header key matches any of default ones (e.g. Accept-Encoding), its value overrides the default one.

Headers(map[string][]string) *http.Response

Does essentially the same as Header().

String(string) *http.Response

Sets the response body to be a string. Any prior body data will be replaced.

Bytes([]byte) *http.Response

Does essentially the same as String().

Write([]byte) (n int, err error)

io.Writer implementation.

TryFile(path string) (*http.Response, error)

tries to open a file by the path for reading and sets it as an upload stream if succeeded. Otherwise, the error is returned.

File(path string) *http.Response

Opens a file by the path and sets it as an upload stream if succeeded. Otherwise, the error is silently written instead. Is a chainable version of TryFile() .

Stream(io.Reader) *http.Response

Sets a reader to be the source of the response's body. If no size is provided AND the reader doesn't have the Len() int method, the stream is considered unsized and therefore will be streamed using chunked transfer encoding. Otherwise, plain transfer is used, unless a compression is applied. Specifying the size of -1 forces the stream to be considered unsized.

Adds cookies. They'll be later rendered as a set of Set-Cookie headers.

TryJSON(any) (*http.Response, error)

Try to serialize the model into JSON. The response is left intact if an error occurred.

JSON(any) *http.Response

Serializes the model into JSON and sets the Content-Type to application/json if succeeded. Otherwise, the error is silently written instead. Is a chainable version of TryJSON().

Error(error, ...status.Code) *http.Response

Returns the response builder with an error set. The nil value for error is a no-op. If the error is an instance of status.HTTPError, its status code is used instead the default one. The default code is status.ErrInternalServerError, which can be overridden if at least one code is specified (all others are ignored).

Expose() response.Fields

Returns a structure containing all the fields set. Useful for middlewares to look into the written response fields.

Clear() *http.Response

Discards all changes made to the response builder.

func myHandler(req *http.Request) *http.Response {
    resp := req.Respond()
    writeSomething(resp)
    return resp
}

Last updated