Skip to content
Go net/http handler to transparently manage posted JSON
Go
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis.yml Initial Commit Mar 19, 2015
README.md Update README.md Mar 20, 2015
example_test.go Initial Commit Mar 19, 2015
formjson.go Handle empty array as existing keys Dec 17, 2015
formjson_test.go Handle empty array as existing keys Dec 17, 2015

README.md

Go JSON handler godoc license build

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:

curl -d name=World :8080

Licenses

All source code is licensed under the MIT License.

You can’t perform that action at this time.