Skip to main content

net/http

Package net/http

provides HTTP client and server implementations.

Integrations

Chain httpin's Middlware to your http.Handlers. We recommend using justinas/alice to chain your middlewares.

Run Demo

package main

import (
"fmt"
"net/http"
"net/http/httptest"

"github.com/ggicci/httpin"
"github.com/justinas/alice"
)

type ListUsersInput struct {
Gender string `in:"query=gender"`
AgeRange []int `in:"query=age_range"`
IsMember bool `in:"query=is_member"`
}

func ListUsers(rw http.ResponseWriter, r *http.Request) {
// Retrieve you data in one line of code!
input := r.Context().Value(httpin.Input).(*ListUsersInput)

fmt.Printf("input: %#v\n", input)
}

func init() {
// Bind input struct with handler.
http.Handle("/users", alice.New(
httpin.NewInput(ListUsersInput{}),
).ThenFunc(ListUsers))
}

func main() {
r, _ := http.NewRequest("GET", "/users?gender=male&age_range=18&age_range=24&is_member=1", nil)

rw := httptest.NewRecorder()
http.DefaultServeMux.ServeHTTP(rw, r)
}