Skip to main content
Version: 2.8.x(Latest)

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:

  1. 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.
  2. The Write*Exit methods append data to the return data buffer and then exit the currently executing HTTP Handler method, which can be used as an alternative to the return method.
  3. The WriteOver* methods are used for buffer overwrite, and the original buffer data will be overwritten with the newly written data.
  4. The WriteStatus* methods are used to set the status code for the current request execution return.
  5. The WriteJson*/ WriteXml methods are used for specific data format output, providing a convenient method for developers.
  6. The WriteTpl* methods are used for template output, parsing and outputting template files, or parsing and outputting the given template content directly.
  7. 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")

Documents

📄️ Response - Exit

Data return control methods in the GoFrame framework, including Exit, ExitAll, and ExitHook. Exit is used to exit the current executing logic method, while ExitAll forcibly interrupts the current execution flow, which is very suitable for permission control. ExitHook is used to control the execution order when multiple HOOK methods are matched to a route. These methods are effective in service functions and HOOK event callback functions and improve usability with minimal runtime overhead.