将URL参数转换为Go中的JSON格式,使用http.Request。

huangapple go评论127阅读模式
英文:

Converting URL Params to JSON in Go with http.Request

问题

我正在验证由请求生成的参数,但与使用 JSON 进行请求时不同,我无法轻松地将查询参数转换为其结构体对应项。使用以下代码:

  1. type ItemsRequest struct {
  2. Item string `json:"item"`
  3. }
  1. func ValidateItemRequest(r *http.Request, w http.ResponseWriter) map[string]interface{} {
  2. var itemRequest ItemsRequest
  3. rules := govalidator.MapData{
  4. "item": []string{"numeric"},
  5. }
  6. opts := govalidator.Options{
  7. Request: r,
  8. Rules: rules,
  9. Data: &itemRequest,
  10. RequiredDefault: true,
  11. }
  12. v := govalidator.New(opts)
  13. e := v.Validate()
  14. // 访问 itemsRequest.item
  15. }

如果我使用 e.ValidateJSON() 并通过请求体传递数据,这样可以工作。

如果我使用 e.Validate() 并通过 URL 参数传递数据,这样就不起作用。

我以为结构体中的 json 标签是问题所在,但是删除它后结果相同。我不认为 Validate() 应该填充结构体,那么我应该如何传递 URL 的值?

我知道我可以使用 r.URL.Query().Get() 手动获取每个值,但在验证了我想要的一切之后,这似乎非常冗余。

英文:

I'm validating parameters made from a request, but unlike when the request is made in JSON, I'm not able to easily convert query params into it's struct counterpart. Using the following:

https://thedevsaddam.medium.com/an-easy-way-to-validate-go-request-c15182fd11b1

  1. type ItemsRequest struct {
  2. Item string `json:"item"`
  3. }
  1. func ValidateItemRequest(r *http.Request, w http.ResponseWriter) map[string]interface{} {
  2. var itemRequest ItemsRequest
  3. rules := govalidator.MapData{
  4. "item": []string{"numeric"},
  5. }
  6. opts := govalidator.Options{
  7. Request: r,
  8. Rules: rules,
  9. Data: &itemRequest ,
  10. RequiredDefault: true,
  11. }
  12. v := govalidator.New(opts)
  13. e := v.Validate()
  14. // access itemsRequest.item
  15. }

If I use e.ValidateJSON() and pass the data in via the body, this works

If I use e.Validate() and pass the data in via url params, this doesn't work.

I assumed the json tag in the struct was the issue, but removing that yields the same result. I don't think Validate() is supposed to populate the struct, but then how am I supposed to pass in the values of the URL?

I know I can use r.URL.Query().Get() to manually pull in each value, but that seems super redundant after I just validated everything I want.

答案1

得分: 1

https://pkg.go.dev/github.com/gorilla/schema 可以为您完成繁重的工作,将 url.Values 转换为 struct,并进行一些模式验证。

它支持类似于 json 的结构字段标签,并且还可以与表单数据或多部分表单数据一起使用。

  1. package main
  2. import (
  3. "github.com/gorilla/schema"
  4. "net/url"
  5. "fmt"
  6. )
  7. type Person struct {
  8. Name string
  9. Phone string
  10. }
  11. func main() {
  12. var decoder = schema.NewDecoder()
  13. u, err := url.Parse("http://localhost/?name=daniel&phone=612-555-4321")
  14. if err != nil { panic(err) }
  15. var person Person
  16. err = decoder.Decode(&person, u.Query())
  17. if err != nil {
  18. // 处理错误
  19. }
  20. fmt.Printf("%+v\n", person)
  21. }
英文:

https://pkg.go.dev/github.com/gorilla/schema can do the heavy lifting for you, converting url.Values into a struct and doing some schema validation as well.

It supports struct field tags much like - and compatible with - json, and can also be used with eg form data or multi-part form data.

  1. package main
  2. import(
  3. "github.com/gorilla/schema"
  4. "net/url"
  5. "fmt"
  6. )
  7. type Person struct {
  8. Name string
  9. Phone string
  10. }
  11. func main() {
  12. var decoder = schema.NewDecoder()
  13. u, err := url.Parse("http://localhost/?name=daniel&phone=612-555-4321")
  14. if err != nil { panic(err) }
  15. var person Person
  16. err = decoder.Decode(&person, u.Query())
  17. if err != nil {
  18. // Handle error
  19. }
  20. fmt.Printf("%+v\n", person)
  21. }

答案2

得分: 0

首先,使用查询结果填充结构体,然后进行验证:

  1. q := request.URL.Query()
  2. myStruct := {
  3. Field1: q.Get("field1"),
  4. Field2: q.Get("field2"),
  5. ...
  6. }
  7. // 处理非字符串字段
  8. // 验证结构体
英文:

First populate the struct with the values from the query, then validate it:

  1. q:=request.URL.Query()
  2. myStruct:= {
  3. Field1: q.Get("field1"),
  4. Field2: q.Get("field2"),
  5. ...
  6. }
  7. // Deal with non-string fields
  8. // Validate the struct

huangapple
  • 本文由 发表于 2021年11月6日 23:11:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/69865199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定