Skip to content
This repository has been archived by the owner on Apr 11, 2020. It is now read-only.

Latest commit

 

History

History
73 lines (50 loc) · 1.46 KB

README.md

File metadata and controls

73 lines (50 loc) · 1.46 KB

elton-body-parser

The middleware has been archived, please use the middleware of elton.

Build Status

Body parser for elton. It support application/json and application/x-www-form-urlencoded type, but NewDefault just support application/json.

package main

import (
	"bytes"

	"github.com/vicanso/elton"
	bodyparser "github.com/vicanso/elton-body-parser"
)

func main() {
	e := elton.New()

	e.Use(bodyparser.NewDefault())

	e.POST("/user/login", func(c *elton.Context) (err error) {
		c.BodyBuffer = bytes.NewBuffer(c.RequestBody)
		return
	})

	e.ListenAndServe(":3000")
}

API

NewDefault

create a new default body parser middleware. It include gzip and json decoder.

e.Use(bodyparser.NewDefault())

NewGzipDecoder

create a new gzip decoder

conf := bodyparser.Config{}
conf.AddDecoder(bodyparser.NewGzipDecoder())
e.Use(bodyparser.New(conf))

NewJSONDecoder

create a new json decoder

conf := bodyparser.Config{}
conf.AddDecoder(bodyparser.NewJSONDecoder())
e.Use(bodyparser.New(conf))

NewFormURLEncodedDecoder

create a new form url encoded decoder

conf := bodyparser.Config{
	ContentTypeValidate: bodyparser.DefaultJSONAndFormContentTypeValidate
}
conf.AddDecoder(bodyparser.NewFormURLEncodedDecoder())
e.Use(bodyparser.New(conf))