responseFormats

原文:https://beego.wiki/docs/mvc/controller/jsonxml/

Response formats 响应格式

JSON, XML, JSONP and YAML JSON、XML、JSONP 和 YAML

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:

​ Beego 还设计用于创建 API 应用程序。当我们构建 API 应用程序时,我们经常需要使用 JSON 或 XML 来响应。Beego 提供了一种简单的方法:

  • Respond with JSON data:

    ​ 使用 JSON 数据响应:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    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.

    ​ ServeJson 将 content-type 设置为 application/json 并对数据进行 JSON 化。

  • Respond with XML data:

    ​ 使用 XML 数据响应:

    1
    2
    3
    4
    5
    
    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.

    ​ ServeXml 将 content-type 设置为 application/xml 并将数据转换为 XML。

  • Respond with jsonp

    ​ 使用 jsonp 响应

    1
    2
    3
    4
    5
    
    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.

    ​ ServeJsonp 将 content-type 设置为 application/javascript ,对数据进行 JSON 化并根据请求参数 callback 响应 jsonp。

  • Renspond based on Accept Header in request

    ​ 根据请求中的 Accept 头部进行响应

    1
    2
    3
    4
    
    func (this *AddController) Get() {
    	mystruct := { ... }
    	this.Resp(mystruct)
    }
    

    Based on the Accept Header value response will be either JSON, XML or YAML. If Accept header is none of the above by default response will be in JSON format

    ​ 根据 Accept 头部值,响应将是 JSON、XML 或 YAML。如果 Accept 头部不是上述任何一种,则默认情况下响应将采用 JSON 格式

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

​ 在 1.6 版本中,方法的名称已更改,从现在开始是 ServeJSON()、ServeXML()、ServeJSONP()。

最后修改 February 4, 2024: 更新 (87c2937)