解析request与构建response

golang gin后端开发框架(二):解析request与构建response 

 

1. 实体绑定

以一个用户注册功能来进行表单实体绑定操作

用户注册需要提交表单数据,假设注册时表单数据包含三项:

  • username
  • phone
  • password

我们创建一个UserRegister结构体用于接收表单数据,通过tag标签的方式设置每个字段对应的form表单中的属性名,通过binding字段设置该属性是否为必须

type UserRegister struct {
    Username string `form:"username" binding:"required"`
    Phone    string `form:"phone" binding:"required"`
    Password string `form:"password" binding:"required"`
}

 

1.1 ShouldBindQuery解析GET参数

使用ShouldBindQuery可以实现Get方法的数据请求绑定,具体实现如下:

1
2
3
4
5
6
7
8
9
10
// get
// http://localhost:8080/register?name=james&phone=8888&password=123456
engine.GET("/register",func(ctx *gin.Context) {
    var user UserRegister
    err:=ctx.ShouldBindQuery(&user)
    if err!=nil{
        log.Fatal(err.Error())
    }
    ctx.Writer.Write([]byte("hello "+user.Username))
})

 

1.2 ShouldBind解析POST参数

1
2
3
4
5
6
7
8
9
10
// post
// http://localhost:8080/register
engine.POST("/register"func(ctx *gin.Context) {
    var user UserRegister
    err := ctx.ShouldBind(&user)
    if err != nil {
        log.Fatal(err.Error())
    }
    ctx.Writer.Write([]byte("hello " + user.Username))
})

 

1.3 BindJSON解析POST请求json格式数据

1
2
3
4
5
6
7
8
9
10
// post
// http://localhost:8080/addstu
engine.POST("/addstu"func(ctx *gin.Context) {
    var stu Student
    err := ctx.BindJSON(&stu)
    if err != nil {
        log.Fatal(err.Error())
    }
    ctx.Writer.Write([]byte("hello " + stu.Name))
})

 

2. 多数据格式返回请求结果

在gin框架中,支持多种返回请求数据格式

 

2.1 []byte

通过context.Writer.Write方法写入byte切片数据

1
ctx.Writer.Write([]byte("hello " + stu.Name))

 

如上面这句代码所示,使用context.Writer.Write向客户端写入返回数据

Writer是gin框架中封装的一个ResponseWriter接口类型,在这个interface中包含了met/http标准库下的ResponseWriter

 

2.2 string

通过context.Writer.WriteString方法写入string类型数据

1
ctx.Writer.WriteString("hello " + stu.Name)

 

2.3 json

gin为了方便开发者更方便地使用,支持将返回数据组装成json格式进行返回

gin框架中的context包含的json方法可以将结构体数据转成json格式的结构化数据,然后返回给客户端

 

2.3.1 map -> json

1
2
3
4
5
6
7
8
9
// get json
// http://localhost:8080/hellojson
engine.GET("/hellojson"func(ctx *gin.Context) {
    ctx.JSON(200, map[string]interface{}{
        "code":    1,
        "message""ok",
        "data":    ctx.FullPath(),
    })
})

 

2.3.2 struct -> json

1
2
3
4
5
6
7
8
9
10
// get json
// http://localhost:8080/hellojson
engine.GET("/hellojson"func(ctx *gin.Context) {
    resp := Response{
        Code:    1,
        Message: "ok",
        Data:    ctx.FullPath(),
    }
    ctx.JSON(200, &resp)
})

 

2.4 html模版和静态资源

当我们需要返回一个html页面或者一些静态资源(图片等)时,gin也提供了一些方法

首先我们需要创建一个html模版,前后端交互的模版语句很简单,就是使用{{}}来表示这是一个模版变量

1
2
{{.title}}
{{.fullPath}}

 

然后我们在后端注册路由,完成html模版的渲染

需要注意的是,gin必须要先设置html目录为可加载状态,才可以向客户端返回html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// get html
// http://localhost:8080/hellohtml
 
// 设置html目录
engine.LoadHTMLGlob("./html/*")
 
// 如果html里包含图片
engine.Static("/img""./img")<br>
engine.GET("/hellohtml"func(ctx *gin.Context) {
    ctx.HTML(http.StatusOK, "index.html", gin.H{
        "title":    "hello gin",
        "fullPath": ctx.FullPath(),
    })
})

在项目开发时,一些静态的资源文件如html、js、css等都可以通过静态资源文件设置的方式来进行设置

posted on 2022-09-01 23:01  myworldworld  阅读(46)  评论(0)    收藏  举报

导航