ytdl-web/pkg/httpx/query.go

30 lines
465 B
Go
Raw Normal View History

2023-08-12 23:27:11 -04:00
package httpx
import (
"errors"
"net/http"
"strconv"
)
var (
ErrQueryKeyNotFound = errors.New("query key not found")
)
func Query(r *http.Request, key string) (string, error) {
values, ok := r.URL.Query()[key]
if !ok || len(values) == 0 {
return "", ErrQueryKeyNotFound
}
return values[0], nil
}
func QueryInt(r *http.Request, key string) (int, error) {
value, err := Query(r, key)
if err != nil {
return 0, err
}
return strconv.Atoi(value)
}