Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rs/formjson
Go net/http handler to transparently manage posted JSON
https://github.com/rs/formjson
Last synced: about 1 month ago
JSON representation
Go net/http handler to transparently manage posted JSON
- Host: GitHub
- URL: https://github.com/rs/formjson
- Owner: rs
- Created: 2015-03-19T23:52:28.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-12-17T09:35:29.000Z (about 9 years ago)
- Last Synced: 2024-11-01T10:42:12.561Z (about 2 months ago)
- Language: Go
- Size: 3.91 KB
- Stars: 39
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-go - formjson - Go net/http handler to transparently manage posted JSON - ★ 30 (Web Frameworks)
- awesome-go-extra - formjson - 03-19T23:52:28Z|2015-12-17T09:35:29Z| (Web Frameworks / Fail injection)
README
# Go JSON handler [![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/rs/formjson) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/rs/formjson/master/LICENSE) [![build](https://img.shields.io/travis/rs/formjson.svg?style=flat)](https://travis-ci.org/rs/formjson)
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
```go
package mainimport (
"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](https://raw.github.com/rs/formjson/master/LICENSE).