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")
Documents
📄️ Response - Buffering
How 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/XML
Using 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 - Redirect
Using 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 - 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.
📄️ Response - File Downloading
Implement 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 Parsing
The 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 - Streaming
Implement 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.