今天来学获取 query, form, cookie 等类型参数的示例,这些例子很贴近实际需求了。
获取 query, form, cookie 等类型参数的示例
获取 query 的例子
直接上代码
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)
func main() {
h := server.Default(server.WithHostPorts("127.0.0.1:8080"))
// Query string parameters are parsed using the existing underlying request object.
// The request responds to url matching: /welcome?firstname=Jane&lastname=Doe&food=apple&food=fish
h.GET("/welcome", func(ctx context.Context, c *app.RequestContext) {
firstname := c.DefaultQuery("firstname", "Guest")
// shortcut for c.Request.URL.Query().Get("lastname")
lastname := c.Query("lastname")
// Iterate all queries and store the one with meeting the conditions in favoriteFood
var favoriteFood []string
c.QueryArgs().VisitAll(func(key, value []byte) {
if string(key) == "food" {
favoriteFood = append(favoriteFood, string(value))
}
})
c.String(consts.StatusOK, "Hello %s %s, favorite food: %s", firstname, lastname, favoriteFood)
})
h.Spin()
}
这个例子里面的 query 预想的应该是:名字,姓氏,然后不定长的食物。处理 query 的逻辑分三段,第一段处理名字,这部分有个默认参数就是 "Guest",第二部分是姓氏,最后一部分是食物,因为食物可以是多个,所以需要一个一个读入,然后添加到数组里。如果成功读到了这个 query,返回的应该就是这里面的名字姓氏和若干个食物名称。
获取 form 的例子
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"fmt"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)
func main() {
h := server.Default(server.WithHostPorts("127.0.0.1:8080"))
// content-type : application/x-www-form-urlencoded
h.POST("/urlencoded", func(ctx context.Context, c *app.RequestContext) {
name := c.PostForm("name")
message := c.PostForm("message")
c.PostArgs().VisitAll(func(key, value []byte) {
if string(key) == "name" {
fmt.Printf("This is %s!", string(value))
}
})
c.String(consts.StatusOK, "name: %s; message: %s", name, message)
})
// content-type : multipart/form-data
h.POST("/formdata", func(ctx context.Context, c *app.RequestContext) {
id := c.FormValue("id")
name := c.FormValue("name")
message := c.FormValue("message")
c.String(consts.StatusOK, "id: %s; name: %s; message: %s\n", id, name, message)
})
h.Spin()
}
这段注册了两个 POST 路由,在第一个路由里,表单数据里面的字符是编码,一眼看不出来是什么。首先读入了名字,然后读入信息,还有另一种读表单的方法,用 VisitAll() 函数,在其中写处理逻辑,自己定义读到什么键做什么操作。如果 POST 请求的表单没问题,最后会打印出来表单里的名字和消息。
第二个 POST 路由里直接得多,直接按照键读对应的值就好了,然后分别打印出这些值。
获取 cookie 的例子
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"fmt"
"time"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
)
func main() {
h := server.Default(server.WithHostPorts("127.0.0.1:8080"))
h.GET("/cookie", func(ctx context.Context, c *app.RequestContext) {
mc := "myCookie"
// get specific key
val := c.Cookie(mc)
if val == nil {
// set a cookie
fmt.Printf("There is no cookie named: %s, and make one...\n", mc)
cookie := protocol.AcquireCookie()
cookie.SetKey("myCookie")
cookie.SetValue("a nice cookie!")
cookie.SetExpire(time.Now().Add(3600 * time.Second))
cookie.SetPath("/")
cookie.SetHTTPOnly(true)
cookie.SetSecure(false)
c.Response.Header.SetCookie(cookie)
protocol.ReleaseCookie(cookie)
c.WriteString("A cookie is ready.")
return
}
fmt.Printf("Got a cookie: %s\nAnd eat it!", val)
// instruct upload_file to delete a cookie
// DelClientCookie instructs the upload_file to remove the given cookie.
// This doesn't work for a cookie with specific domain or path,
// you should delete it manually like:
//
// c := AcquireCookie()
// c.SetKey(mc)
// c.SetDomain("example.com")
// c.SetPath("/path")
// c.SetExpire(CookieExpireDelete)
// h.SetCookie(c)
// ReleaseCookie(c)
//
c.Response.Header.DelClientCookie(mc)
// construct the full struct of a cookie in response's header
respCookie := protocol.AcquireCookie()
respCookie.SetKey(mc)
c.Response.Header.Cookie(respCookie)
fmt.Printf("(The expire time of cookie is set to: %s)\n", respCookie.Expire())
protocol.ReleaseCookie(respCookie)
c.WriteString("The cookie has been eaten.")
})
h.Spin()
}
这段代码里首先手动创建了一个 cookie,然后调用 DelClientCookie() 把这个 cookie 删除了,同时也在注释里提醒了对于一个指定域名或者路径的 cookie 是需要我们手动删除的,最后写了一大段完整结构的对于 cookie 的回应,同时也删除了使用过的 cookie。