目錄
一、Iris框架的請求處理方式
1、GET請求及獲取參數
(1)處理Get請求
(2)處理Get請求 並接受參數
http://localhost:7999/hello?username="張三"&pwd="123456"
(3)正則表達式路由
Iris框架在進行處理http請求時,支持請求url中包含正則表達式。
正則表達式的具體規則爲:
- 1、使用{}對增則表達式進行包裹,url中出現類似{}樣式的格式,即識別爲正則表達式
- 2、支持自定義增則表達式的變量的命名,變量名用字母表示。比如:{name}
- 3、支持對自定義正則表達式變量的數據類型限制,變量名和對應的數據類型之間用“:”分隔開。比如:{name:string}表示增則表達式爲name,類型限定爲string類型
- 4、通過context.Params()的Get()和GetXxx()系列方法來獲取對應的請求url中的增則表達式的變量
- 5、增則表達式支持變量的數據類型包括:string、int、uint、bool等
2、POST請求及獲取參數
(1)處理Post請求 form表單的字段獲取
postman設置及請求如下:
(2)處理Post請求 Json格式數據
postman設置及請求如下:
Postman工具選擇[{"key":"Content-Type","value":"application/json","description":""}]
請求內容:{"name": "davie","age": 28}
(3)處理Post請求 Xml格式數據
postman設置及請求如下:
Postman工具選擇[{"key":"Content-Type","value":"application/xml","description":""}]
請求內容:
<Person>
<Name>大大</Name>
<Age>56</Age>
</Person>
3、PUT請求
4、DELETE請求
5、完整代碼
(1)model
package model
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
(2)main函數
func main() {
app := iris.New()
//url: http://localhost:8000/getRequest
//type:GET請求
app.Get("/getRequest", func(context context.Context) {
//獲取path
path := context.Path()
//日誌輸出
app.Logger().Info(path)
//寫入返回數據
context.WriteString(path)
})
app.Handle("GET", "/hello", 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>")
})
//處理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, "==", pwd)
})
app.Post("/postJson", func(context context.Context) {
path := context.Path()
app.Logger().Info("請求URL:",path)
//2.Json數據解析
var person model.Person
//通過context.ReadJSON()讀取傳過來的數據
if err := context.ReadJSON(&person); err != nil {
panic(err.Error())
}
//返回格式化的內容
context.Writef("Received: %#+v\n", person)
})
app.Post("/postXml", func(context context.Context) {
//1.Path
path := context.Path()
app.Logger().Info("請求URL:", path)
//2.XML數據解析
var person model.Person
if err := context.ReadXML(&person); err != nil {
panic(err.Error())
}
//返回格式化的內容
context.Writef("Received:%#+v\n", person)
})
//url:http://localhost:8000/user/info
//type:POST請求,Handle方法第一個參數爲POST,表明是Post請求
app.Handle("POST", "/user/info", func(context context.Context) {
context.WriteString(" User Info is Post Request , Deal is in handle func ")
})
//put請求
app.Put("/putinfo", func(context context.Context) {
path := context.Path()
app.Logger().Info("請求url:", path)
})
//delete請求
app.Delete("/deleteuser", func(context context.Context) {
path := context.Path()
app.Logger().Info("Delete請求url:", path)
})
app.Run(iris.Addr("localhost:7999"))
}
二、請求處理的數據格式返回
返回string類型數據
context.WriteString("hello world")
返回json格式的數據
context.JSON(iris.Map{"message": "hello word", "requestCode": 200})
返回xml格式的數據
context.XML(Person{Name: "Davie", Age: 18})
返回html格式數據
context.HTML(" Davie, 12 ")