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

  1. func (r *Response) Write(content ...interface{})
  2. func (r *Response) WriteExit(content ...interface{})
  3. func (r *Response) WriteJson(content interface{}) error
  4. func (r *Response) WriteJsonExit(content interface{}) error
  5. func (r *Response) WriteJsonP(content interface{}) error
  6. func (r *Response) WriteJsonPExit(content interface{}) error
  7. func (r *Response) WriteOver(content ...interface{})
  8. func (r *Response) WriteOverExit(content ...interface{})
  9. func (r *Response) WriteStatus(status int, content ...interface{})
  10. func (r *Response) WriteStatusExit(status int, content ...interface{})
  11. func (r *Response) WriteTpl(tpl string, params ...gview.Params) error
  12. func (r *Response) WriteTplContent(content string, params ...gview.Params) error
  13. func (r *Response) WriteTplDefault(params ...gview.Params) error
  14. func (r *Response) WriteXml(content interface{}, rootTag ...string) error
  15. func (r *Response) WriteXmlExit(content interface{}, rootTag ...string) error
  16. func (r *Response) Writef(format string, params ...interface{})
  17. func (r *Response) WritefExit(format string, params ...interface{})
  18. func (r *Response) Writefln(format string, params ...interface{})
  19. func (r *Response) WriteflnExit(format string, params ...interface{})
  20. func (r *Response) Writeln(content ...interface{})
  21. 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:

  1. Response.Header().Set("Content-Type", "text/plain; charset=utf-8")

Documentation

📄️ Response - BufferingHow to achieve data response buffering control in the GoFrame framework. Using a buffer can improve execution efficiency and provide more flexible output control. Example code demonstrates how to handle return data uniformly through middleware, avoiding direct exposure of error messages to clients, and providing customized error message prompts.

📄️ Response - JSON/XMLUsing the Response object of the GoFrame framework to implement data return functions, supporting JSON and XML format output. Through methods such as WriteJson and WriteXml, content output can be easily achieved. Sample code demonstrates how to use GoFrame to build an HTTP server and provide implementation with support for the JSONP protocol.

📄️ Response - RedirectUsing RedirectTo and RedirectBack methods in the GoFrame framework to implement page redirect functions. It realizes page-to-page redirection through Location Header, including redirecting to a specified address and returning to the previous page. This example demonstrates how to set up page redirection in a local service to help developers understand HTTP address handling and the application of the Referer Header.

📄️ Response - ExitData 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.

📄️ Response - File DownloadingImplement file downloading in applications built with the GoFrame framework through the Response object. The ServeFile method can display file contents, while the ServeFileDownload method guides the client to download the specified file path, fully utilizing streaming download technology to reduce memory usage and improve performance.

📄️ Response - Template ParsingThe operation of using Response methods for template parsing and data returns in the GoFrame framework, including methods such as WriteTpl and ParseTpl. Through these methods, template files or content can be parsed and output, while also supporting built-in variables such as Config, Cookie, and Session, providing flexible template operation. It also includes detailed example code to help you better understand and apply.

📄️ Response - StreamingImplement HTTP streaming data returns using the GoFrame framework, suitable for framework versions less than v2.4 and above. Achieve efficient streaming data transmission with simplified code, applicable for scenarios requiring long connections and continuous data updates, and provides notices and references.