Skip to main content

Getting Started

httpin is a Go package for decoding HTTP requests into structs. We can decode

and more of an HTTP request into a struct in Go.

Install

go get github.com/ggicci/httpin

Thanks 🥰

If you thought this package helpful, please consider giving it a big star ⭐️. Thanks in advance!

Quick View

Before using httpin

func ListUsers(rw http.ResponseWriter, r *http.Request) {
page, err := strconv.ParseInt(r.FormValue("page"), 10, 64)
if err != nil {
// Invalid parameter: page.
return
}
perPage, err := strconv.ParseInt(r.FormValue("per_page"), 10, 64)
if err != nil {
// Invalid parameter: per_page.
return
}
isMember, err := strconv.ParseBool(r.FormValue("is_member"))
if err != nil {
// Invalid parameter: is_member.
return
}

// Do sth.
}

Using httpin

type ListUsersInput struct {
Page int `in:"query=page"`
PerPage int `in:"query=per_page"`
IsMember bool `in:"query=is_member"`
}

func ListUsers(rw http.ResponseWriter, r *http.Request) {
input := r.Context().Value(httpin.Input).(*ListUsersInput)
// Do sth.
}

Comparison

ItemsBefore (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😞 HighLower
Code Readability😟 PoorHighly readable
Maintainability😡 Poor😍 Highly maintainable

🌟 Example Project

You could visit https://github.com/ggicci/httpin-example/blob/main/main.go for the complete code.