1
0
mirror of https://github.com/ggicci/httpin.git synced 2024-11-24 08:32:45 +02:00
🍡 HTTP Input for Go - Decode an HTTP request into a custom struct 📢 the encoding feature will come soon, see branch feat/encoder https://ggicci.github.io/httpin/
Go to file
2022-11-10 00:59:52 +08:00
.github Merge pull request #34 from ggicci/dependabot/github_actions/actions/setup-go-3 2022-05-16 11:22:29 +08:00
internal feat: support decoding float formatted timestamp to time.Time 2022-07-23 08:59:36 +08:00
patch a simple code 2022-09-28 10:43:55 +08:00
.gitignore feat: add default directive 2022-04-16 14:39:13 +08:00
body_test.go feat: allow registering custom body decoders 2022-05-31 11:55:53 +08:00
body.go chore: comment 2022-05-31 12:33:07 +08:00
decoder_test.go feat: introduce a "decoder" directive to specify a field decoder 2022-07-23 16:17:47 +08:00
decoders.go feat: introduce a "decoder" directive to specify a field decoder 2022-07-23 16:17:47 +08:00
default_test.go feat: add default directive 2022-04-16 14:39:13 +08:00
default.go feat: add default directive 2022-04-16 14:39:13 +08:00
directives_test.go feat: introduce a "decoder" directive to specify a field decoder 2022-07-23 16:17:47 +08:00
directives.go feat: introduce a "decoder" directive to specify a field decoder 2022-07-23 16:17:47 +08:00
errors.go fix: allow unwrapping fieldError 2022-08-06 18:32:21 +04:00
extractor.go feat: introduce a "decoder" directive to specify a field decoder 2022-07-23 16:17:47 +08:00
file_test.go feat: eliminate rarely used apis 2021-12-08 23:44:11 +08:00
file.go feat: add default directive 2022-04-16 14:39:13 +08:00
form_test.go feat: add default directive 2022-04-16 14:39:13 +08:00
form.go feat: add default directive 2022-04-16 14:39:13 +08:00
go.mod Merge pull request #33 from ggicci/dependabot/go_modules/github.com/go-chi/chi/v5-5.0.7 2022-04-07 11:04:51 +08:00
go.sum Merge pull request #33 from ggicci/dependabot/go_modules/github.com/go-chi/chi/v5-5.0.7 2022-04-07 11:04:51 +08:00
gochi_test.go refactor: gochi tests 2021-07-16 16:48:32 +08:00
gochi.go feat: add default directive 2022-04-16 14:39:13 +08:00
gorilla_test.go feat: integrate with gorilla mux package to extract path varialbes 2021-06-24 14:08:13 +08:00
gorilla.go feat: add default directive 2022-04-16 14:39:13 +08:00
header.go feat: add default directive 2022-04-16 14:39:13 +08:00
httpin_test.go fix: allow unwrapping fieldError 2022-08-06 18:32:21 +04:00
httpin.go feat: introduce a "decoder" directive to specify a field decoder 2022-07-23 16:17:47 +08:00
LICENSE Initial commit 2021-04-13 10:15:37 +08:00
Makefile refactor: update ci scripts 2021-04-23 11:26:31 +08:00
middleware_test.go feat: ReplaceDefaultErrorHandler 2021-11-30 14:52:33 +08:00
middleware.go feat: allow registering custom body decoders 2022-05-31 11:55:53 +08:00
options_test.go refactor: extract logic update 2021-12-08 11:21:10 +08:00
options.go feat: introduce a "decoder" directive to specify a field decoder 2022-07-23 16:17:47 +08:00
query_test.go feat: add default directive 2022-04-16 14:39:13 +08:00
query.go feat: add default directive 2022-04-16 14:39:13 +08:00
README.md chore(docs): up README alternatives sections 2022-11-10 00:59:52 +08:00
required_test.go feat: add default directive 2022-04-16 14:39:13 +08:00
required.go feat: add default directive 2022-04-16 14:39:13 +08:00
resolver_test.go feat: introduce a "decoder" directive to specify a field decoder 2022-07-23 16:17:47 +08:00
resolver.go feat: introduce a "decoder" directive to specify a field decoder 2022-07-23 16:17:47 +08:00

httpin logo

httpin - HTTP Input for Go

Decode an HTTP request into a custom struct

Core Features

httpin helps you easily decoding HTTP request data from

  • Query parameters, e.g. ?name=john&is_member=true
  • Headers, e.g. Authorization: xxx
  • Form data, e.g. username=john&password=******
  • JSON/XML Body, e.g. POST {"name":"john"}
  • Path variables, e.g. /users/{username}
  • File uploads

You only need to define a struct to receive/bind data from an HTTP request, without writing any parsing stuff code by yourself.

How to use?

type ListUsersInput struct {
	Token    string `in:"query=access_token;header=x-access-token"`
	Page     int    `in:"query=page;default=1"`
	PerPage  int    `in:"query=per_page;default=20"`
	IsMember bool   `in:"query=is_member"`
}

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

	if input.IsMember {
		// Do sth.
	}
	// Do sth.
}

httpin is:

Why this package?

Compared with using net/http package

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.
}
Benefits 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

Alternatives and Similars