GitHub - rs/formjson: Go net/http handler to transparently manage posted JSON (original) (raw)

FormJSON is a net/http handler implementing content negotiation for posted data in order to transparently expose posted JSON as if it was application/x-www-form-urlencoded. The posted data is then available using built-in r.FormValue("key")'s http.Request method.

To match capabilities of application/x-www-form-urlencoded, only single depth JSON object with string as keys and values is supported.

Usage

package main

import ( "net/http" "fmt"

"github.com/rs/formjson"

)

func main() { h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { body := fmt.Sprintf("Hello %s!", r.FormValue("name")) w.Write([]byte(body)) })

handler := formjson.Handler(h)
http.ListenAndServe(":8080", handler)

}

Then this request:

curl -H "Content-Type:application/json" -d '{"name":"World"}' :8080

is now equivalent to this one:

Licenses

All source code is licensed under the MIT License.