2021-04-13 07:33:04 +02:00
# httpin
2022-01-27 13:06:20 +02:00
[![Go ](https://github.com/ggicci/httpin/actions/workflows/go.yml/badge.svg?branch=main )](https://github.com/ggicci/httpin/actions/workflows/go.yml) [![documentation ](https://github.com/ggicci/httpin/actions/workflows/documentation.yml/badge.svg?branch=documentation )](https://github.com/ggicci/httpin/actions/workflows/documentation.yml) [![codecov ](https://codecov.io/gh/ggicci/httpin/branch/main/graph/badge.svg?token=RT61L9ngHj )](https://codecov.io/gh/ggicci/httpin) [![Go Reference ](https://pkg.go.dev/badge/github.com/ggicci/httpin.svg )](https://pkg.go.dev/github.com/ggicci/httpin) [![Go Report Card ](https://goreportcard.com/badge/github.com/ggicci/httpin )](https://goreportcard.com/report/github.com/ggicci/httpin)
2021-06-24 09:12:18 +02:00
2021-10-27 09:00:21 +02:00
HTTP Input for Go - < b > Decode an HTTP request into a custom struct< / b >
2021-05-08 10:15:26 +02:00
2021-10-27 09:00:21 +02:00
< table >
< tr >
< td align = "center" >
< a href = "https://ggicci.github.io/httpin/" >
< img src = "https://docusaurus.io/img/docusaurus.svg" height = "48px" / >
< / a >
< / td >
< / tr >
< tr >
< td >
< a href = "https://ggicci.github.io/httpin/" > Documentation< / a >
< / td >
< / tr >
< / table >
2021-04-29 05:55:02 +02:00
2021-05-08 08:07:09 +02:00
## Quick View
2021-04-29 05:55:02 +02:00
2021-05-08 08:07:09 +02:00
< table >
< tr >
2021-10-27 09:00:21 +02:00
< th > Before (use net/http)< / th >
< th > After (use httpin)< / th >
2021-05-08 08:07:09 +02:00
< / tr >
< tr >
< td >
2021-04-29 05:55:02 +02:00
2021-05-08 08:07:09 +02:00
```go
func ListUsers(rw http.ResponseWriter, r *http.Request) {
page, err := strconv.ParseInt(r.FormValue("page"), 10, 64)
if err != nil {
2021-05-10 09:35:02 +02:00
// Invalid parameter: page.
2021-05-08 08:07:09 +02:00
return
}
perPage, err := strconv.ParseInt(r.FormValue("per_page"), 10, 64)
if err != nil {
2021-05-10 09:35:02 +02:00
// Invalid parameter: per_page.
return
}
isMember, err := strconv.ParseBool(r.FormValue("is_member"))
if err != nil {
// Invalid parameter: is_member.
2021-05-08 08:07:09 +02:00
return
}
2021-05-10 09:35:02 +02:00
// Do sth.
2021-05-08 08:07:09 +02:00
}
```
2021-04-29 05:55:02 +02:00
2021-05-08 08:07:09 +02:00
< / td >
2021-05-10 09:36:06 +02:00
< td >
2021-05-08 08:07:09 +02:00
```go
type ListUsersInput struct {
2021-10-10 10:01:29 +02:00
Page int `in:"query=page"`
PerPage int `in:"query=per_page"`
IsMember bool `in:"query=is_member"`
2021-05-08 08:07:09 +02:00
}
func ListUsers(rw http.ResponseWriter, r *http.Request) {
2021-07-15 11:43:02 +02:00
input := r.Context().Value(httpin.Input).(*ListUsersInput)
2021-05-10 09:35:02 +02:00
// Do sth.
2021-05-08 08:07:09 +02:00
}
```
< / td >
< / tr >
< / table >
2021-10-27 09:00:21 +02:00
## Why this package?
2021-05-08 16:52:58 +02:00
2021-10-27 09:00:21 +02:00
| Items | Before (use net/http package) | After (use ggicci/httpin package) |
| -------------------- | ------------------------------------------ | ---------------------------------------------------------------------------------------------- |
| Developer Time | 😫 Expensive (too much parsing stuff code) | 🚀 **Faster** (define the struct for receiving input data and leave the parsing job to httpin) |
| Code Repetition Rate | 😞 High | **Lower** |
| Code Readability | 😟 Poor | **Highly readable** |
| Maintainability | 😡 Poor | 😍 **Highly maintainable** |