Beego Learning Notes 13:Api Writing

Api Writing 1> The common data formats used by api are...

Api Writing

1> The common data formats used by api are json and xml.

2> Here's how different data formats are used

1->JSON data direct output.

After calling ServeJSON, the content-type is set to application/json, and the data is serialized as JSON output

2->Direct output of XML data

After calling ServeXML, the content-type is set to application/xml, and the data is serialized for XML output

3->jsonp call

After calling ServeJSONP, the content-type is set to application/javascript, the data is serialized as JSON, and the jsonp output is set according to the requested callback parameter.

4->Dictionary tabular data

In the form of key-value pairs

3> Create a new oneApi.goController to write business logic.The code is as follows:

package controllers import ( "github.com/astaxie/beego" ) //Api Page type ApiController struct { beego.Controller } func (c *ApiController) Get() { c.TplName="api.html" } //Data in JSON format type ApiJsonController struct { beego.Controller } func (c *ApiJsonController) Get() { //Note that the json here must be a json c.Data["json"] = "ABCDEFG" c.ServeJSON() } //Data in XML format type ApiXMLController struct { beego.Controller } func (c *ApiXMLController) Get() { //Note that the xml here must be xml c.Data["xml"] = "BCDEFGH" c.ServeXML() } //Data in Jsonp format type ApiJsonpController struct { beego.Controller } func (c *ApiJsonpController) Get() { //Note that the jsonp here must be jsonp c.Data["jsonp"] = "CDEFGHI" c.ServeJSONP() } //Dictionary tabular data type ApiDictionaryController struct { beego.Controller } func (c *ApiDictionaryController) Get() { c.Data["json"]=map[string]interface{}{"name":"ABC123","rows":45,"flag":true}; c.ServeJSON() } //Tabular data with parameters type ApiParamsController struct { beego.Controller } func (c *ApiParamsController) Get() { search:=c.GetString("name") c.Data["json"]=map[string]interface{}{"name":search,"rows":45,"flag":false}; c.ServeJSON() }

4> Create a new oneApi.htmlPage, used as test page

<!DOCTYPE html> <html> <head> <title>home page - User List Page</title> <link rel="shortcut icon" href="/static/img/favicon.png" /> <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.css"/> <script type="text/javascript" src="/static/js/jquery-2.1.1.min.js"></script> <script type="text/javascript" src="/static/bootstrap/js/bootstrap.min.js"></script> </head> <body> <div> <!--Requested Json data--> <div style="width:100%;height:50px;"> <button onclick="getjson()">obtain Json</button> <label id="txtjson"></label> </div> <!--Requested Xml data--> <div style="width:100%;height:50px;"> <button onclick="getxml()">obtain Xml</button> <label id="txtxml"></label> </div> <!--Requested Jsonp data--> <div style="width:100%;height:50px;"> <button onclick="getjsonp()">obtain Jsonp</button> <label id="txtjsonp"></label> </div> <!--Request Dictionary Data--> <div style="width:100%;height:50px;"> <button onclick="getdictionary()">Get Dictionary</button> <label id="txtdictionary"></label> </div> <!--Request Dictionary Data--> <div style="width:100%;height:50px;"> <input type="text" id="search" placeholder="Please enter parameters"/> <button onclick="getparams()">Get parameters</button> <label id="txtparams"></label> </div> </div> <!--JS Part--> <script type="text/javascript"> //Get Json function getjson(){ $.ajax({ type:'get', url:'/api/GetJson', dataType:'json',//Here is the format of the json data data:{}, success:function(result){ console.log('Obtain json Data') console.log(result) $("#Txtjson'). HTML ('json result:'+result'); } }) } //Get Xml function getxml(){ $.ajax({ type:'get', url:'/api/GetXml', dataType:'xml',//Here is the format of the xml data data:{}, success:function(result){ console.log('Obtain xml Data') console.log(result) $("#Txtxml'). HTML ('XML result:'+$(result). text ()'); } }) } //Get jsonp function getjsonp(){ $.ajax({ type:'get', url:'/api/GetJsonp', dataType:'jsonp',//Here is the format of the jsonp data data:{}, success:function(result){ console.log('Obtain jsonp Data') console.log(result) $("#Txtjsonp'). HTML ('jsonp result:'+result'); } }) } //Get Dictionary function getdictionary(){ $.ajax({ type:'get', url:'/api/GetDictionary',//Here is the format of the json data data:{}, success:function(result){ console.log('Get Dictionary Data') console.log(result) $("#Txtdictionary'). HTML ('result of dictionary:'+Result.name+ ","+Result.rows+ ","+Result.flag; } }) } //Get parameters function getparams(){ $.ajax({ type:'get', url:'/api/GetParams',//Here is the format of the json data data:{ "name":$("#search").val() }, success:function(result){ console.log('Get data for parameters') console.log(result.json) $("#txtparams").html(" Gets the parameter result: "+Result.name+ ","+Result.rows+ ","+Result.flag; } }) } </script> </body> </html>

5> Add routes to routers, compile runs, fix errors

package routers import ( "secondweb/controllers" "github.com/astaxie/beego" ) func init() { beego.Router("/", &controllers.MainController{}) beego.Router("/Home/PageData", &controllers.UserController{}) beego.Router("/Home/PageNextData", &controllers.YonghuController{}) beego.Router("/Home/Index", &controllers.PageController{}) beego.Router("/Home/EasyUI", &controllers.EasyUIController{}) beego.Router("/Home/EasyUIData", &controllers.EasyUIDataController{}) beego.Router("/Home/FileOpt", &controllers.FileOptUploadController{}) beego.Router("/Home/FileDown", &controllers.FileOptDownloadController{}) beego.Router("/Home/FileRead", &controllers.ReadController{}) beego.Router("/Home/FileWrite", &controllers.WriteController{}) beego.Router("/Home/FileCreate", &controllers.CreateController{}) beego.Router("/Home/FileDelete", &controllers.DeleteController{}) //Api interface section beego.Router("/api/Html", &controllers.ApiController{}) beego.Router("/api/GetJson", &controllers.ApiJsonController{}) beego.Router("/api/GetXml", &controllers.ApiXMLController{}) beego.Router("/api/GetJsonp", &controllers.ApiJsonpController{}) beego.Router("/api/GetDictionary", &controllers.ApiDictionaryController{}) beego.Router("/api/GetParams", &controllers.ApiParamsController{}) }

6> Running Effect

1->Run the following pages:

2->Call json's interface to get data in JSON format

3->Request data in xml format to get data in xml format

4->Request data in jsonp format to get data in jsonp format

5->Request data in dictionary table format to get data in key-value pair format

6->Request data for parameters, return parameters and other data

7> The next chapter is about the use of session s

26 June 2020, 12:14 | Views: 5300

Add new comment

For adding a comment, please log in
or create account

0 comments