在 go 中验证 http 请求的惯用方法

Lea*_*rng 6 validation http go

我需要验证我的 http 请求是否有两个参数:Start 和 End。目前,我设置了一个不应显示为任一参数的默认值,并检查它和其他无效值。然而,这感觉就像是黑客攻击。执行此操作的正确方法应该是什么?

这是我的代码:

type Request struct {
    Start int `json: "start"`
    End int `json: "end"`
}


func HandlePost(w http.ResponseWriter, r *http.Request) {
    body , _ := ioutil.ReadAll(r.Body)
    reqData := Request{Start: -1, End: -1} // < whats the correct way to do this
    json.Unmarshal(body, &reqData)  

    if reqData.Start < 0 && reqData.End < 0 {
        w.WriteHeader(http.StatusBadRequest)
        return
    }
    // rest of the logic
}
Run Code Online (Sandbox Code Playgroud)

ret*_*oot 0

这是使用结构标记和指针验证结构的另一种方法。请注意,如果 0 是要传递的有效值,则此解决方案将不起作用。omitempty 认为 0 值为空。如果您希望此功能正常工作,请将 0 视为有效,删除指针并修改 IsValid 方法

package main

import (
    "encoding/json"
    "fmt"
)

type Request struct {
    Start *int `json: "start,omitempty"`
    End   *int `json: "end,omitempty"`
}

func (r Request) IsValid() (bool, error) {
    if r.Start == nil {
        return false, fmt.Errorf("start is missing")
    }

    if r.End == nil {
        return false, fmt.Errorf("end is missing")
    }

    return true, nil
}

var (
    invalidStartb = `{"end": 1}`
    invalidEndb   = `{"start": 1}`
    valid         = `{"start": 1, "end": 1}`
)

func main() {
    var r Request

    _ = json.Unmarshal([]byte(invalidStartb), &r)
    fmt.Println(r.IsValid())

    r = Request{}
    _ = json.Unmarshal([]byte(invalidEndb), &r)
    fmt.Println(r.IsValid())

    r = Request{}
    _ = json.Unmarshal([]byte(valid), &r)
    fmt.Println(r.IsValid())

}
Run Code Online (Sandbox Code Playgroud)

可运行版本在这里https://goplay.space/#Z0eqLpEHO37