diff --git a/controller.go b/controller.go index 7098c1b5..71287811 100644 --- a/controller.go +++ b/controller.go @@ -25,6 +25,13 @@ import ( "github.com/astaxie/beego/utils" ) +//commonly used mime-types +const ( + applicationJson = "application/json" + applicationXml = "applicatoin/xml" + textXml = "text/xml" +) + var ( // custom error when user stop request handler manually. USERSTOPRUN = errors.New("User stop run") @@ -287,6 +294,20 @@ func (c *Controller) ServeXml() { c.Ctx.Output.Xml(c.Data["xml"], hasIndent) } +// ServeFormatted serve Xml OR Json, depending on the value of the Accept header + +func (c *Controller) ServeFormatted() { + accept := c.Ctx.Input.Header("Accept") + switch accept { + case applicationJson: + c.ServeJson() + case applicationXml, textXml: + c.ServeXml() + default: + c.ServeJson() + } +} + // Input returns the input data map from POST or PUT request body and query string. func (c *Controller) Input() url.Values { if c.Ctx.Request.Form == nil {