go初识iris框架(二) - get,post请求和数据格式

初步了解iris

获取url路径

package main
  
import "github.com/kataras/iris/v12"

func main(){
    
    
  app := iris.New()

  app.Get("/hello",func(ctx iris.Context){
    
    
    path := context.Path()
    app.Logger().Info(path)
    context.WriteString("请求路径",path)
  })
  app.Listen(":82")
}

在这里插入图片描述

获取数据

get请求

app.Get("/userinfo",func(ctx iris.Context){
    
    
    //path := ctx.Path()
    userName := ctx.URLParam("username")
    pwd := ctx.URLParam("pwd")

    if(userName=="admin"&&pwd=="123456"){
    
    
      ctx.HTML("<h1>"+userName+"  welcome" + "</h1>")
    }else{
    
    
      ctx.HTML("<h1>"+"plase register again"+"</h1>")
	}
})

post请求

app.Post("/postLogin",func(ctx iris.Context){
    
    
    name := ctx.PostValue("name")
    pwd := ctx.PostValue("pwd")
    app.Logger().Info(name,"   ",pwd)
    ctx.HTML(name)
})

在这里插入图片描述

获取JSON数据格式

iris.Context.ReadJSON() //用来读取

JSON返回值

iris.Context.JSON()

获取XML数据格式

iris.Context.ReadXML()

XML返回值

iris.Context.XML()

猜你喜欢

转载自blog.csdn.net/yasinawolaopo/article/details/131763709