Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
48 lines (37 sloc) 1.14 KB
name sort
Response formats
8

JSON, XML and JSONP

Beego is also designed for the creation of API applications. When we build an API application, we often need to respond with JSON or XML. Beego provides a simple approach:

  • Respond with JSON data:

     type mystruct struct {
       FieldOne string `json:"field_one"`
     }
     
     func (this *AddController) Get() {
     	mystruct := { ... }
     	this.Data["json"] = &mystruct
     	this.ServeJSON()
     }

    ServeJson will set content-type to application/json and JSONify the data.

  • Respond with XML data:

     func (this *AddController) Get() {
     	mystruct := { ... }
     	this.Data["xml"]=&mystruct
     	this.ServeXML()
     }

    ServeXml will set content-type to application/xml and convert the data into XML.

  • Respond with jsonp

     func (this *AddController) Get() {
     	mystruct := { ... }
     	this.Data["jsonp"] = &mystruct
     	this.ServeJSONP()
     }

    ServeJsonp will set content-type to application/javascript , JSONify the data and respond to jsonp based on the request parameter callback.

In version 1.6 names of methods were changed, it is ServeJSON(), ServeXML(), ServeJSONP() from now on.