beego: controller add ServeFormatted
ServeFormatted serve Xml OR Json, depending on the value of the Accept header
Showing
1 changed file
with
21 additions
and
0 deletions
| ... | @@ -25,6 +25,13 @@ import ( | ... | @@ -25,6 +25,13 @@ import ( |
| 25 | "github.com/astaxie/beego/utils" | 25 | "github.com/astaxie/beego/utils" |
| 26 | ) | 26 | ) |
| 27 | 27 | ||
| 28 | //commonly used mime-types | ||
| 29 | const ( | ||
| 30 | applicationJson = "application/json" | ||
| 31 | applicationXml = "applicatoin/xml" | ||
| 32 | textXml = "text/xml" | ||
| 33 | ) | ||
| 34 | |||
| 28 | var ( | 35 | var ( |
| 29 | // custom error when user stop request handler manually. | 36 | // custom error when user stop request handler manually. |
| 30 | USERSTOPRUN = errors.New("User stop run") | 37 | USERSTOPRUN = errors.New("User stop run") |
| ... | @@ -287,6 +294,20 @@ func (c *Controller) ServeXml() { | ... | @@ -287,6 +294,20 @@ func (c *Controller) ServeXml() { |
| 287 | c.Ctx.Output.Xml(c.Data["xml"], hasIndent) | 294 | c.Ctx.Output.Xml(c.Data["xml"], hasIndent) |
| 288 | } | 295 | } |
| 289 | 296 | ||
| 297 | // ServeFormatted serve Xml OR Json, depending on the value of the Accept header | ||
| 298 | |||
| 299 | func (c *Controller) ServeFormatted() { | ||
| 300 | accept := c.Ctx.Input.Header("Accept") | ||
| 301 | switch accept { | ||
| 302 | case applicationJson: | ||
| 303 | c.ServeJson() | ||
| 304 | case applicationXml, textXml: | ||
| 305 | c.ServeXml() | ||
| 306 | default: | ||
| 307 | c.ServeJson() | ||
| 308 | } | ||
| 309 | } | ||
| 310 | |||
| 290 | // Input returns the input data map from POST or PUT request body and query string. | 311 | // Input returns the input data map from POST or PUT request body and query string. |
| 291 | func (c *Controller) Input() url.Values { | 312 | func (c *Controller) Input() url.Values { |
| 292 | if c.Ctx.Request.Form == nil { | 313 | if c.Ctx.Request.Form == nil { | ... | ... |
-
Please register or sign in to post a comment