Commit 1da37f6c authored by astaxie's avatar astaxie

beego: controller add ServeFormatted

ServeFormatted serve Xml OR Json, depending on the value of the Accept
header
parent ef6d9b9a
...@@ -25,6 +25,13 @@ import ( ...@@ -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() { ...@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment