數(shù)據(jù)請求方式的分類
所有的項目中使用的請求都遵循HTTP協(xié)議標準,HTTP協(xié)議經(jīng)過了1.0和1.1兩個版本的發(fā)展。
HTTP1.0定義了三種請求方法: GET, POST 和 HEAD方法。
HTTP1.1新增了五種請求方法:OPTIONS, PUT, DELETE, TRACE 和 CONNECT 方法。
因此,我們可以說,HTTP協(xié)議一共定義了八種方法用來對Request-URI網(wǎng)絡(luò)資源的不同操作方式,這些操作具體為:GET、POST、PUT、DELETE、HEAD、OPTIONS、TRACE、CONNECT等八種操作方式。
Iris框架的請求處理方式?
1、Iris框架中服務(wù)實例app中包含多個方法,用來支持對上述HTTP多種請求類型的直接處理,直接定義為get方法、post方法、put方法等,app中包含的自動處理路由請求的方法與http請求類型的分類一致。
2、除了上述1中自動處理各類別的請求外,框架還支持使用通用的Handle方法來自定義編寫自己的請求處理類型及對應(yīng)的方法。?
兩種路由請求的處理方式示例如下:?
app := iris.New()
?
//url: http://localhost:8000/getRequest
//type:GET請求
app.Get("/getRequest", func(context context.Context) {
? ? ? ? path := context.Path()
? ? ? ? app.Logger().Info(path)
})
?
//url: http://localhost:/user/info
//type:POST請求
app.Handle("POST", "/user/info", func(context context.Context) {
? ? ? ? context.WriteString(" User Info is Post Request , Deal is in handle func ")
})
?
//啟動端口監(jiān)聽服務(wù)
app.Run(iris.Addr(":8000"))
?
GET請求
向特定的網(wǎng)絡(luò)資源數(shù)據(jù)發(fā)起請求。GET請求可以攜帶請求數(shù)據(jù),攜帶的請求數(shù)據(jù)會以?分割URL和傳輸數(shù)據(jù),參數(shù)之間以&相連,比如http://localhost:3000?name=davie&pwd=123。如下是一個http的get類型的請求:?
http://localhost:8000/userpath
服務(wù)端的路由處理方式如下:?
//url:http://localhost:8000/userpath
//type:GET請求、用GET方法處理
app.Get("/userpath", func(context context.Context) {
?
? ? ? ? //獲取Path
? ? ? ? path := context.Path()
? ? ? ? //日志輸出
? ? ? ? app.Logger().Info(path)
? ? ? ? //寫入返回數(shù)據(jù):string類型
? ? ? ? context.WriteString("請求路徑:" + path)
})
上述為使用已經(jīng)封裝的默認的app.Get方式來處理請求,使用Handle方法來進行處理,如下所示:?
//url:http://localhost:8000/hello
//type:GET請求、Handle方法第一個參數(shù)為GET,表明是GET請求方式
app.Handle("GET", "/hello", func(context context.Context) {
? ? ? ? context.HTML("
Hello world.
")})
POST請求
POST請求在進行請求時會將請求數(shù)據(jù)放在請求body中進行請求,請求數(shù)據(jù)大小沒有限制。在開發(fā)過程中,我們使用postman工具來進行POST請求的調(diào)試。POST請求的示例如下所示:?
http://localhost:8000/postLogin
服務(wù)端的路由處理方式如下:?
//type:POST請求
//攜帶數(shù)據(jù):name、pwd命名的請求數(shù)據(jù)
app.Post("/postLogin", func(context context.Context) {
? ? ? ? //獲取請求path
? ? ? ? path := context.Path()
? ? ? ? //日志
? ? ? ? app.Logger().Info(path)
? ? ? ? //獲取請求數(shù)據(jù)字段
? ? ? ? name := context.PostValue("name")
? ? ? ? pwd, err := context.PostValueInt("pwd")
? ? ? ? if err != nil {
? ? ? ? ? ? panic(err.Error())
? ? ? ? }
? ? ? ? app.Logger().Info(name, "? ", pwd)
? ? ? ? //返回
? ? ? ? context.HTML(name)
})
上述為使用默認路由請求方法Post方法來進行處理,同時,還可以使用Handle方法來進行處理,如下圖:
//url:http://localhost:8000/user/info
//type:POST請求,Handle方法第一個參數(shù)為POST,表明是Post請求
app.Handle("POST", "/user/info", func(context context.Context) {
? ? ? ? context.WriteString(" User Info is Post Request , Deal is in handle func ")
})
PUT、DELETE、OPTIONS、HEAD等其他類型請求?
除了上述GET、POST最為常見的兩種請求方式以外,還有PUT、DELETE、OPTIONS、HEAD等其他類型請求,對于其他類型的請求,如同GET和POST請求一樣,都是可以通過兩種方式來進行處理:?
1、iris框架提供的自動識別請求類型的處理請求方法,如put方法、head方法、options方法、delete方法等
2、使用通用的Handle方法對路由請求進行處理,開發(fā)者自己選擇具體的請求類型以、對應(yīng)url和要進行處理的func。?
如下是put和delete的請求處理: PUT請求
//type:PUT類型請求
app.Put("/putinfo", func(context context.Context) {
? ? ? ? path := context.Path()
? ? ? ? app.Logger().Info("請求url:", path)
})
DELETE請求
//type:DELETE類型請求 ?
app.Delete("/deleteuser", func(context context.Context) {
? ? ? ? path := context.Path()
? ? ? ? app.Logger().Info("Delete請求url:", path)
})
請求處理的數(shù)據(jù)格式返回?
在本節(jié)課程中,我們已經(jīng)學(xué)習(xí)了如何對不同類型的請求進行處理以及如何獲取請求所攜帶的數(shù)據(jù),當后臺接收到請求后,將會對請求進行處理,處理完畢后將數(shù)據(jù)返回給請求的客戶端。接下來,我們看一看如何將數(shù)據(jù)進行返回,以及都有哪些形式。?
在進行請求處理時,處理方法func有一個參數(shù)context。Context是用于處理請求的上下文環(huán)境變量,用于處理http請求及相關(guān)數(shù)據(jù)返回。iris框架支持多種數(shù)據(jù)格式的返回,此處我們學(xué)習(xí)掌握返回string、json、xml以及html格式的數(shù)據(jù)。
返回string類型數(shù)據(jù)?
context.WriteString("hello world")
返回json格式的數(shù)據(jù)?
context.JSON(iris.Map{"message": "hello word", "requestCode": 200})
返回xml格式的數(shù)據(jù)?
context.XML(Person{Name: "Davie", Age: 18})
返回html格式數(shù)據(jù)?
context.HTML("<h1> Davie, 12 </h1>")
通過本節(jié)課的內(nèi)容,我們學(xué)習(xí)了iris框架中的不同類型的數(shù)據(jù)請求以及返回不同的數(shù)據(jù)格式。