Iris请求方式和数据返回类型
1. Iris起服务
package main
import "github.com/kataras/iris"
func main() {
//1.创建app结构体对象
app := iris.New() //返回一个application
//2.端口监听(启动服务本质就是监听端口)
//iris.WithoutServerError 设置错误
app.Run(iris.Addr(":7999"), iris.WithoutServerError(iris.ErrServerClosed))
//也可以不设置错误
//application.Run(iris.Addr(":8080"))
//application.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed)) //第二种
}
2. 数据请求方式的分类
所有的项目中使用的请求都遵循HTTP协议标准,HTTP协议经过了1.0和1.1两个版本的发展。
-
HTTP1.0定义了三种请求方法: GET, POST 和 HEAD方法。
-
HTTP1.1新增了五种请求方法:OPTIONS, PUT, DELETE, TRACE 和 CONNECT 方法。
因此,我们可以说,HTTP协议一共定义了八种方法用来对Request-URI网络资源的不同操作方式,这些操作具体为:GET、POST、PUT、DELETE、HEAD、OPTIONS、TRACE、CONNECT等八种操作方式。
请求方式:
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)
func main() {
app := iris.New()
//第一个参数是路径
//第二个是匿名函数,处理请求
app.Get("/getRequest", func(context context.Context) {
//处理get请求,请求的url为:/getRequest
path := context.Path()
//以日志的方式打印
app.Logger().Info(path) //2020/02/05 07:17 /getRequest
})
//1.处理Get请求
app.Get("/userpath", func(context context.Context) {
//获取Path
path := context.Path()
app.Logger().Info(path) //2020/02/05 07:18 /userpath
//写入返回数据:string类型
context.WriteString("请求路径:" + path)
})
//2.处理Get请求 并接受参数
//http://localhost:8000/userinfo?username=yang&pwd=123456
app.Get("/userinfo", func(context context.Context) {
path := context.Path()
app.Logger().Info(path)
//获取get请求所携带的参数
userName := context.URLParam("username")
app.Logger().Info(userName)
pwd := context.URLParam("pwd")
app.Logger().Info(pwd)
//返回html数据格式
context.HTML("<h1>" + userName + "," + pwd + "</h1>")
})
//3.处理Post请求 form表单的字段获取
app.Post("/postLogin", func(context context.Context) {
path := context.Path()
app.Logger().Info(path)
//context.PostValue方法来获取post请求所提交的form表单数据
name := context.PostValue("name")
pwd := context.PostValue("pwd")
app.Logger().Info(name, " ", pwd)
context.HTML(name)
})
//4.处理Post请求 Json格式数据
//Postman工具选择[{"key":"Content-Type","value":"application/json","description":""}]
//请求内容:{"name": "davie","age": 28}
app.Post("/postJson", func(context context.Context) {
//1.path
path := context.Path()
app.Logger().Info("请求URL:", path)
//2.Json数据解析
var person Person
//通过context.ReadJSON()读取传过来的数据
if err := context.ReadJSON(&person); err != nil {
panic(err.Error())
}
//返回格式化的内容
context.Writef("Received: %#+v\n", person)
})
//5.处理Post请求 Xml格式数据
/*
* 请求配置:Content-Type到application/xml(可选但最好设置)
* 请求内容:
*
* <student>
* <stu_name>davie</stu_name>
* <stu_age>28</stu_age>
* </student>
*
*/
app.Post("/postXml", func(context context.Context) {
//1.Path
path := context.Path()
app.Logger().Info("请求URL:", path)
//2.XML数据解析
var student Student
if err := context.ReadXML(&student); err != nil {
panic(err.Error())
}
//返回格式化的内容
context.Writef("Received:%#+v\n", student)
})
//6.put请求
app.Put("/putinfo", func(context context.Context) {
path := context.Path()
app.Logger().Info("请求url:", path)
})
//7.delete请求
app.Delete("/deleteuser", func(context context.Context) {
path := context.Path()
app.Logger().Info("Delete请求url:", path)
})
app.Run(iris.Addr(":8000"), iris.WithoutServerError(iris.ErrServerClosed))
}
//自定义的struct
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
//自定义的结构体
type Student struct {
//XMLName xml.Name `xml:"student"`
StuName string `xml:"stu_name"`
StuAge int `xml:"stu_age"`
}
type config struct {
Addr string `yaml:"addr"`
ServerName string `yaml:"serverName"`
}
数据返回类型:
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)
type Teacher struct {
Name string `json:"Name"`
City string `json:"City"`
Other string `json:"Other"`
}
//程序主入口
func main() {
app := iris.New()
/**
* 通过Get请求
* 返回 WriteString
*/
app.Get("/getHello", func(context context.Context) {
context.WriteString(" Hello world ")
})
/**
* 通过Get请求
* 返回 HTML数据
*/
app.Get("/getHtml", func(context context.Context) {
context.HTML("<h1> Davie, 12 </h1>")
})
/**
* 通过Get请求
* 返回 Json数据
*/
app.Get("/getJson", func(context context.Context) {
context.JSON(iris.Map{"message": "hello word", "requestCode": 200})
})
//POST
app.Post("/user/postinfo", func(context context.Context) {
//context.WriteString(" Post Request ")
//user := context.Params().GetString("user")
var tec Teacher
err := context.ReadJSON(&tec)
if err != nil {
panic(err.Error())
} else {
app.Logger().Info(tec)
context.WriteString(tec.Name)
}
//fmt.Println(user)
//teacher := Teacher{}
//err := context.ReadForm(&teacher)
//if err != nil {
// panic(err.Error())
//} else {
// context.WriteString(teacher.Name)
//}
})
//PUT
app.Put("/getHello", func(context context.Context) {
context.WriteString(" Put Request ")
})
app.Delete("/DeleteHello", func(context context.Context) {
context.WriteString(" Delete Request ")
})
//返回json数据
app.Get("/getJson", func(context context.Context) {
context.JSON(iris.Map{"message": "hello word", "requestCode": 200})
})
app.Get("/getStuJson", func(context context.Context) {
context.JSON(Student{Name: "Davie", Age: 18})
})
//返回xml数据
app.Get("/getXml", func(context context.Context) {
context.XML(Person{Name: "Davie", Age: 18})
})
//返回Text
app.Get("/helloText", func(context context.Context) {
context.Text(" text hello world ")
})
app.Run(iris.Addr(":8000"), iris.WithoutServerError(iris.ErrServerClosed))
}
//json结构体
type Student struct {
Name string `json:"name"`
Age int `json:"age"`
}
//xml结构体
type Person struct {
Name string `xml:"name"`
Age int `xml:"age"`
}

浙公网安备 33010602011771号