【问题标题】:How to check if URL param exists如何检查 URL 参数是否存在
【发布时间】:2019-12-10 04:15:14
【问题描述】:

在进行某种类型的验证之前,我必须检查 URL 参数是否存在。我该怎么做?

如果参数像这样为空:http://myurl.com?myparam= 那么 myParam == "" 为真,但如果 url 以这种方式出现 http://myurl.com(没有参数),那么 myParam == "" 也是真的......所以我需要一些方法来验证参数是否在 url 中

# example
# http://myurl.com?myparam=johndoe

// validate if param exists, here i dont know how to do
#
#


// then do some validation
func validateMyParamIsNotNumber(r *http.Request, resultMessage *string) {
    myParam := r.FormValue("myparam")

    if myParam != ""  && isNotNumber(product) {
        *resultMessage = "The myparam filter must be a number"
        return
    }
}

【问题讨论】:

    标签: go gorilla mux


    【解决方案1】:

    使用map index with multiple assignment 检查Request.Form 中是否存在密钥。在检查地图之前解析表单。

    func validateMyParamIsNotNumber(r *http.Request, resultMessage *string) {
        r.ParseForm()
        _, hasMyParam := r.Form["myparam"]
        ...
    

    【讨论】:

      猜你喜欢
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 2022-06-17
      相关资源
      最近更新 更多