ResponseWriter
12345678910111213type ResponseWriter interface { Header() Header
WriteHeader(code int)
Write(p []byte) (int, error)
Close() error
SetManualFramingMode(bool)
Append(other io.ReadCloser) error}ResponseWriter is used to respond to client requests.
Methods
Append
View sourceAppend a body onto the end of this response. Will fail if passed anything other than a Response's Body field. This operation is performed in amortized constant time, and so should always be preferred to directly copying a body with io.Copy.
Parameters
- other: io.ReadCloser
Returns
Close
View sourceClose() errorClose the response to the client. The ResponseWriter is automatically closed after the request handler finishes running, so it is not necessary to call it explicitly, but doing so indicates that the response can be fully written to the client immediately.
If WriteHeader has not yet been called, Close calls WriteHeader(200) before closing the response. Once closed, a ResponseWriter is invalidated, and may no longer be accessed or used in any way.
Returns
Header
View sourceHeader returns the headers that will be sent by WriteHeader. The Header map also is the mechanism with which implementations can set HTTP trailers. Changing the returned headers after a call to WriteHeader has no effect unless the modified headers are Trailers.
There are two ways to set Trailers. The preferred way is to predeclare in the headers which trailers you will later send by setting the "Trailer" header to the names of the trailer keys which will come later. In this case, those keys of the Header map are treated as if they were trailers. See the example. The second way, for trailer keys not known to the Handler until after the first ResponseWriter.Write, is to prefix the Header map keys with the [TrailerPrefix] constant value.
Returns
SetManualFramingMode
View sourceSetManualFramingMode(bool)SetManualFramingMode controls how the framing headers (Content-Length/Transfer-Encoding) are set for a response.
If set to true, the response uses the exact framing headers set in the message. If set to false, or set to true and the framing is invalid, the framing headers are based on the message body, and any framing headers already set in the message are discarded.
To have an effect on the response, this must be called before any call to Write() or WriteHeader().
Parameters
- : bool
Write
View sourceWrite(p []byte) (int, error)Write the data to the connection as part of an HTTP reply.
If WriteHeader has not yet been called, Write calls WriteHeader(200) before writing the data. Unlike the ResponseWriter in net/http, Write will not automatically add Content-Type or Content-Length headers.
Parameters
- p: []byte
Returns
WriteHeader
View sourceWriteHeader(code int)WriteHeader initiates the response to the client by sending an HTTP response preamble with the provided status code, and all of the response headers collected by Header. If WriteHeader is not called explicitly, the first call to Write or Close will trigger an implicit call to WriteHeader with a code of 200. After the first call to WriteHeader, subsequent calls will print a warning but have no effect.
1xx status codes other than 103 (Early Hints) are not permitted and will result in an ErrInvalidStatusCode error when calling Write, Close, or Append, and clients will receive a 500 (Internal Server Error) response.
Parameters
- code: int