1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-23 02:10:57 +00:00

beego: controller add ServeFormatted

ServeFormatted serve Xml OR Json, depending on the value of the Accept
header
This commit is contained in:
astaxie 2014-05-17 01:26:59 +08:00
parent 31a63c5d50
commit 6bdf0838ce

View File

@ -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 {