Introduction
The data return of the HTTP Server
is implemented through the ghttp.Response
object, which implements the standard library’s http.ResponseWriter
API. Data output is achieved using the Write*
related methods, and the data output utilizes a Buffer
mechanism, thus the processing efficiency of the data is relatively high. At any time, you can output buffered data to the client and clear buffer data through the OutputBuffer
method.
Common methods: For a more detailed list of APIs, please refer to https://pkg.go.dev/github.com/gogf/gf/v2/net/ghttp#Response
func (r *Response) Write(content ...interface{})
func (r *Response) WriteExit(content ...interface{})
func (r *Response) WriteJson(content interface{}) error
func (r *Response) WriteJsonExit(content interface{}) error
func (r *Response) WriteJsonP(content interface{}) error
func (r *Response) WriteJsonPExit(content interface{}) error
func (r *Response) WriteOver(content ...interface{})
func (r *Response) WriteOverExit(content ...interface{})
func (r *Response) WriteStatus(status int, content ...interface{})
func (r *Response) WriteStatusExit(status int, content ...interface{})
func (r *Response) WriteTpl(tpl string, params ...gview.Params) error
func (r *Response) WriteTplContent(content string, params ...gview.Params) error
func (r *Response) WriteTplDefault(params ...gview.Params) error
func (r *Response) WriteXml(content interface{}, rootTag ...string) error
func (r *Response) WriteXmlExit(content interface{}, rootTag ...string) error
func (r *Response) Writef(format string, params ...interface{})
func (r *Response) WritefExit(format string, params ...interface{})
func (r *Response) Writefln(format string, params ...interface{})
func (r *Response) WriteflnExit(format string, params ...interface{})
func (r *Response) Writeln(content ...interface{})
func (r *Response) WritelnExit(content ...interface{})
Brief Explanation:
- The
Write*
methods are used to append data to the return data buffer. The parameters can be of any data format, and they are automatically analyzed through assertions. - The
Write*Exit
methods append data to the return data buffer and then exit the currently executingHTTP Handler
method, which can be used as an alternative to thereturn
method. - The
WriteOver*
methods are used for buffer overwrite, and the original buffer data will be overwritten with the newly written data. - The
WriteStatus*
methods are used to set the status code for the current request execution return. - The
WriteJson*
/WriteXml
methods are used for specific data format output, providing a convenient method for developers. - The
WriteTpl*
methods are used for template output, parsing and outputting template files, or parsing and outputting the given template content directly. - For other methods, see the API documentation;
Additionally, it is worth mentioning that Header
operations can be implemented using standard library methods, for example:
Response.Header().Set("Content-Type", "text/plain; charset=utf-8")