github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/api/util/requests.go (about)

     1  package util
     2  
     3  import (
     4  	"net/http"
     5  	"strconv"
     6  )
     7  
     8  // ReqParamInt extracts an integer parameter from a request form value
     9  func ReqParamInt(r *http.Request, key string, def int) int {
    10  	v := r.FormValue(key)
    11  	if v == "" {
    12  		return def
    13  	}
    14  	i, err := strconv.ParseInt(v, 10, 0)
    15  	if err != nil {
    16  		return def
    17  	}
    18  	return int(i)
    19  }
    20  
    21  // ReqParamBool pulls a boolean parameter from a request form value
    22  func ReqParamBool(r *http.Request, key string, def bool) bool {
    23  	v := r.FormValue(key)
    24  	if v == "" {
    25  		return def
    26  	}
    27  	b, err := strconv.ParseBool(v)
    28  	if err != nil {
    29  		return def
    30  	}
    31  	return b
    32  }