# httpin [![codecov](https://codecov.io/gh/ggicci/httpin/branch/main/graph/badge.svg?token=RT61L9ngHj)](https://codecov.io/gh/ggicci/httpin) HTTP Input for Go - Decode an HTTP request into a custom struct **Define your input struct and then fetch your data!** ## Quick View
BEFORE (use net/http) | AFTER (use httpin) |
---|---|
```go func ListUsers(rw http.ResponseWriter, r *http.Request) { page, err := strconv.ParseInt(r.FormValue("page"), 10, 64) if err != nil { // ... invalid page return } perPage, err := strconv.ParseInt(r.FormValue("per_page"), 10, 64) if err != nil { // ... invalid per_page return } isVip, _ := strconv.ParseBool(r.FormValue("is_vip")) // do sth. } ``` | ```go type ListUsersInput struct { Page int `in:"form=page"` PerPage int `in:"form=per_page"` IsVip bool `in:"form=is_vip"` } func ListUsers(rw http.ResponseWriter, r *http.Request) { interfaceInput, err := httpin.New(ListUsersInput{}).Decode(r) if err != nil { // err can be *httpin.InvalidField return } input := interfaceInput.(*ListUsersInput) // do sth. } ``` |