github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_response_write.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 // 7 8 package ghttp 9 10 import ( 11 "fmt" 12 "github.com/gogf/gf/internal/json" 13 "net/http" 14 15 "github.com/gogf/gf/encoding/gparser" 16 "github.com/gogf/gf/util/gconv" 17 ) 18 19 // Write writes <content> to the response buffer. 20 func (r *Response) Write(content ...interface{}) { 21 if r.hijacked || len(content) == 0 { 22 return 23 } 24 if r.Status == 0 { 25 r.Status = http.StatusOK 26 } 27 for _, v := range content { 28 switch value := v.(type) { 29 case []byte: 30 r.buffer.Write(value) 31 case string: 32 r.buffer.WriteString(value) 33 default: 34 r.buffer.WriteString(gconv.String(v)) 35 } 36 } 37 } 38 39 // WriteExit writes <content> to the response buffer and exits executing of current handler. 40 // The "Exit" feature is commonly used to replace usage of return statement in the handler, 41 // for convenience. 42 func (r *Response) WriteExit(content ...interface{}) { 43 r.Write(content...) 44 r.Request.Exit() 45 } 46 47 // WriteOver overwrites the response buffer with <content>. 48 func (r *Response) WriteOver(content ...interface{}) { 49 r.ClearBuffer() 50 r.Write(content...) 51 } 52 53 // WriteOverExit overwrites the response buffer with <content> and exits executing 54 // of current handler. The "Exit" feature is commonly used to replace usage of return 55 // statement in the handler, for convenience. 56 func (r *Response) WriteOverExit(content ...interface{}) { 57 r.WriteOver(content...) 58 r.Request.Exit() 59 } 60 61 // Writef writes the response with fmt.Sprintf. 62 func (r *Response) Writef(format string, params ...interface{}) { 63 r.Write(fmt.Sprintf(format, params...)) 64 } 65 66 // WritefExit writes the response with fmt.Sprintf and exits executing of current handler. 67 // The "Exit" feature is commonly used to replace usage of return statement in the handler, 68 // for convenience. 69 func (r *Response) WritefExit(format string, params ...interface{}) { 70 r.Writef(format, params...) 71 r.Request.Exit() 72 } 73 74 // Writeln writes the response with <content> and new line. 75 func (r *Response) Writeln(content ...interface{}) { 76 if len(content) == 0 { 77 r.Write("\n") 78 return 79 } 80 r.Write(append(content, "\n")...) 81 } 82 83 // WritelnExit writes the response with <content> and new line and exits executing 84 // of current handler. The "Exit" feature is commonly used to replace usage of return 85 // statement in the handler, for convenience. 86 func (r *Response) WritelnExit(content ...interface{}) { 87 r.Writeln(content...) 88 r.Request.Exit() 89 } 90 91 // Writefln writes the response with fmt.Sprintf and new line. 92 func (r *Response) Writefln(format string, params ...interface{}) { 93 r.Writeln(fmt.Sprintf(format, params...)) 94 } 95 96 // WriteflnExit writes the response with fmt.Sprintf and new line and exits executing 97 // of current handler. The "Exit" feature is commonly used to replace usage of return 98 // statement in the handler, for convenience. 99 func (r *Response) WriteflnExit(format string, params ...interface{}) { 100 r.Writefln(format, params...) 101 r.Request.Exit() 102 } 103 104 // WriteJson writes <content> to the response with JSON format. 105 func (r *Response) WriteJson(content interface{}) error { 106 // If given string/[]byte, response it directly to client. 107 switch content.(type) { 108 case string, []byte: 109 r.Header().Set("Content-Type", "application/json") 110 r.Write(gconv.String(content)) 111 return nil 112 } 113 // Else use json.Marshal function to encode the parameter. 114 if b, err := json.Marshal(content); err != nil { 115 return err 116 } else { 117 r.Header().Set("Content-Type", "application/json") 118 r.Write(b) 119 } 120 return nil 121 } 122 123 // WriteJsonExit writes <content> to the response with JSON format and exits executing 124 // of current handler if success. The "Exit" feature is commonly used to replace usage of 125 // return statement in the handler, for convenience. 126 func (r *Response) WriteJsonExit(content interface{}) error { 127 if err := r.WriteJson(content); err != nil { 128 return err 129 } 130 r.Request.Exit() 131 return nil 132 } 133 134 // WriteJson writes <content> to the response with JSONP format. 135 // 136 // Note that there should be a "callback" parameter in the request for JSONP format. 137 func (r *Response) WriteJsonP(content interface{}) error { 138 // If given string/[]byte, response it directly to client. 139 switch content.(type) { 140 case string, []byte: 141 r.Header().Set("Content-Type", "application/json") 142 r.Write(gconv.String(content)) 143 return nil 144 } 145 // Else use json.Marshal function to encode the parameter. 146 if b, err := json.Marshal(content); err != nil { 147 return err 148 } else { 149 //r.Header().Set("Content-Type", "application/json") 150 if callback := r.Request.GetString("callback"); callback != "" { 151 buffer := []byte(callback) 152 buffer = append(buffer, byte('(')) 153 buffer = append(buffer, b...) 154 buffer = append(buffer, byte(')')) 155 r.Write(buffer) 156 } else { 157 r.Write(b) 158 } 159 } 160 return nil 161 } 162 163 // WriteJsonPExit writes <content> to the response with JSONP format and exits executing 164 // of current handler if success. The "Exit" feature is commonly used to replace usage of 165 // return statement in the handler, for convenience. 166 // 167 // Note that there should be a "callback" parameter in the request for JSONP format. 168 func (r *Response) WriteJsonPExit(content interface{}) error { 169 if err := r.WriteJsonP(content); err != nil { 170 return err 171 } 172 r.Request.Exit() 173 return nil 174 } 175 176 // WriteXml writes <content> to the response with XML format. 177 func (r *Response) WriteXml(content interface{}, rootTag ...string) error { 178 // If given string/[]byte, response it directly to client. 179 switch content.(type) { 180 case string, []byte: 181 r.Header().Set("Content-Type", "application/xml") 182 r.Write(gconv.String(content)) 183 return nil 184 } 185 // Else use gparser.VarToXml function to encode the parameter. 186 if b, err := gparser.VarToXml(content, rootTag...); err != nil { 187 return err 188 } else { 189 r.Header().Set("Content-Type", "application/xml") 190 r.Write(b) 191 } 192 return nil 193 } 194 195 // WriteXmlExit writes <content> to the response with XML format and exits executing 196 // of current handler if success. The "Exit" feature is commonly used to replace usage 197 // of return statement in the handler, for convenience. 198 func (r *Response) WriteXmlExit(content interface{}, rootTag ...string) error { 199 if err := r.WriteXml(content, rootTag...); err != nil { 200 return err 201 } 202 r.Request.Exit() 203 return nil 204 } 205 206 // WriteStatus writes HTTP <status> and <content> to the response. 207 // Note that do not set Content-Type header here. 208 func (r *Response) WriteStatus(status int, content ...interface{}) { 209 r.WriteHeader(status) 210 if len(content) > 0 { 211 r.Write(content...) 212 } else { 213 r.Write(http.StatusText(status)) 214 } 215 } 216 217 // WriteStatusExit writes HTTP <status> and <content> to the response and exits executing 218 // of current handler if success. The "Exit" feature is commonly used to replace usage of 219 // return statement in the handler, for convenience. 220 func (r *Response) WriteStatusExit(status int, content ...interface{}) { 221 r.WriteStatus(status, content...) 222 r.Request.Exit() 223 }