Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joshbetz/bodyparser
Automatically parse request.Body depending on the Content-Type header
https://github.com/joshbetz/bodyparser
body-parser go golang
Last synced: 7 days ago
JSON representation
Automatically parse request.Body depending on the Content-Type header
- Host: GitHub
- URL: https://github.com/joshbetz/bodyparser
- Owner: joshbetz
- License: mit
- Created: 2017-04-01T17:06:49.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2021-11-12T16:58:22.000Z (about 3 years ago)
- Last Synced: 2024-06-20T00:31:46.877Z (5 months ago)
- Topics: body-parser, go, golang
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 5
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bodyparser
[![Build Status](https://travis-ci.org/joshbetz/bodyparser.svg?branch=master)](https://travis-ci.org/joshbetz/bodyparser) [![Go Report Card](https://goreportcard.com/badge/github.com/joshbetz/bodyparser)](https://goreportcard.com/report/github.com/joshbetz/bodyparser) [![](https://godoc.org/github.com/joshbetz/bodyparser?status.svg)](http://godoc.org/github.com/joshbetz/bodyparser)
Automatically parse the net/http request.Body into request.Form data depending on the Content-Type header.
## Example
```go
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 := bodyparser.Middleware(h)
http.ListenAndServe(":3000", handler)
}
```## API
```go
func Middleware(h http.Handler) http.Handler
```A middleware for net/http to parse request bodies into request.Form data
depending on the content type.```go
func Parse(r *http.Request) (map[string]interface{}, error)
```Similar to `Middleware`, but instead of fitting the data into request.Form,
returns a `map[string]interface{}`. Useful for parsing more complex data than
string to string.