In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use automatic verification of restful api request parameters". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Deserialization of http api request body
In the process of deserialization, go-zero implements its own set of deserialization mechanism according to the data format and data verification requirements of the requested data.
1.1 data format take the order order.api file as an example, type (createOrderReq struct {token string `path: "token" `/ / user token productId string `json: "productId"` / / Commodity ID num int `json: "num" `/ / quantity of goods} createOrderRes struct { Success bool `json: "success" `/ / successful} findOrderReq struct {token string `path: "token"` / user token page int `form: "page" `/ / number of pages pageSize int8 `form: "pageSize"` / / Page size} findOrderRes struct { OrderInfo [] orderInfo `json: "orderInfo" `/ / Commodity ID} orderInfo struct {productId string `json: "productId"` / / Commodity ID productName string `json: "productName" `/ / Commodity name num int `json: "num"` / / Commodity quantity} deleteOrderReq struct { Id string `path: "id" `} deleteOrderRes struct {success bool `json: "success"` / / successful}) service order {@ doc (summary: create an order) @ handler CreateOrderHandler post / order/add/:token (createOrderReq) returns (createOrderRes) @ doc (summary: get an order) @ handler FindOrderHandler get / order/find/:token (findOrderReq) returns (findOrderRes) @ doc (summary: delete order) @ handler: DeleteOrderHandler delete / order/:id (deleteOrderReq) returns (deleteOrderRes)}
There are three types of tag for deserialization of the http api request body:
Parameter deserialization in path:http url path
/ order/add/1234567 will resolve that token is 1234567
Form:http form form deserialization requires header header to add Content-Type: multipart/form-data
/ order/find/1234567?page=1&pageSize=20 will parse out that the token is 1234567, the page is 1, the pageSize is 20.
Json:http request json body deserialization requires header header to add Content-Type: application/json
{"productId": "321", "num": 1} will resolve that productId is 321 and productId is 1.
1.2 data verification take user user.api file as an example type (createUserReq struct {age int8 `json: "age,default=20,range= (12createUserReq struct 100]) `/ / age name string `json:" name "` / / name alias string `json:" alias Optional "`/ / alias sex string `json:" sex,options=male | female "` / / gender avatar string `json:" avatar Default=default.png "`/ / profile picture} createUserRes struct {success bool `json:" success "` / / successful}) service user {@ doc (summary: create order) @ handler CreateUserHandler post / user/add (createUserReq) returns (createUserRes)}
There are many ways to validate data, including the following but not limited:
Age: do not enter 20 by default, and the range of input value is (12 100), which is opened before and closed later.
Name: required, not empty
Alias: optional, but empty
Sex: required. Value is male or female
Avatar: optional. Default is default.png.
That's all for "how to use automatic validation of restful api request parameters". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.