1
0
mirror of https://github.com/astaxie/beego.git synced 2024-06-29 11:54:14 +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" "github.com/astaxie/beego/utils"
) )
//commonly used mime-types
const (
applicationJson = "application/json"
applicationXml = "applicatoin/xml"
textXml = "text/xml"
)
var ( var (
// custom error when user stop request handler manually. // custom error when user stop request handler manually.
USERSTOPRUN = errors.New("User stop run") USERSTOPRUN = errors.New("User stop run")
@ -287,6 +294,20 @@ func (c *Controller) ServeXml() {
c.Ctx.Output.Xml(c.Data["xml"], hasIndent) 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. // Input returns the input data map from POST or PUT request body and query string.
func (c *Controller) Input() url.Values { func (c *Controller) Input() url.Values {
if c.Ctx.Request.Form == nil { if c.Ctx.Request.Form == nil {