form

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2020 License: BSD-3-Clause Imports: 7 Imported by: 3

README

Form Decoder Build Status Coverage Status Go Report Card GoDoc Release

A form deocder that decode request body of any types(xml, json, form, multipart form...) into a scturct by same codebase.

Usage

By default, form decoder can handles the following content types:

  • Form(application/x-www-form-urlencoded)
  • Multipart Form(multipart/form-data)
  • JSON(application/json)
  • XML(application/xml)

Register allow to register particular decoder or replace default decoder for the specified content type.

package main

import (
	"fmt"
	"net/http"

	"github.com/clevergo/clevergo"
	"github.com/clevergo/form"
)

type User struct {
	Username string `json:"username" xml:"username"`
	Password string `json:"password" xml:"password"`
}

func login(w http.ResponseWriter, r *http.Request) {
	user := User{}
	err := form.Decode(r, &user)
	if err != nil {
		http.Error(w, err.Error(), http.StatusForbidden)
		return
	}
	fmt.Fprintf(w, "username: %s, password: %s", user.Username, user.Password)
}

func main() {
	app := clevergo.New("localhost:1234")
	app.Post("/login", login)
	app.ListenAndServe()
}
$ curl -XPOST -d "username=foo&password=bar"  http://localhost:1234/login
username: foo, password: bar

$ curl -XPOST -H "Content-Type: application/json" -d '{"username":"foo", "password": "bar"}' http://localhost:1234/login
username: foo, password: bar

$ curl -XPOST -H "Content-Type: application/xml" -d '<xml><username>foo</username><password>bar</password></xml>' http://localhost:1234/login
username: foo, password: bar

$ curl -XPOST -F "username=foo" -F "password=bar" http://localhost:1234/login
username: foo, password: bar

Documentation

Overview

Package form is a form decoder that decode request body into a struct.

Index

Constants

View Source
const (
	ContentType              = "Content-Type"
	ContentTypeForm          = "application/x-www-form-urlencoded"
	ContentTypeMultipartForm = "multipart/form-data"
	ContentTypeJSON          = "application/json"
	ContentTypeXML           = "application/xml"
)

Content type constants.

Variables

This section is empty.

Functions

func Decode

func Decode(r *http.Request, v interface{}) error

Decode data from a request into v, v should be a pointer.

func JSON

func JSON(r *http.Request, v interface{}) error

JSON is a JSON decoder.

func Register

func Register(contentType string, decoder Decoder)

Register a decoder for the given content type.

func XML

func XML(r *http.Request, v interface{}) error

XML is an XML decoder.

Types

type Decoder

type Decoder func(req *http.Request, v interface{}) error

Decoder is a function that decode data from request into v.

func NewForm

func NewForm(decoder *schema.Decoder) Decoder

NewForm returns a post form decoder with the given schema decoder.

func NewMultipartForm

func NewMultipartForm(maxMemory int64) Decoder

NewMultipartForm returns a multipart form decoder with the given schema decoder.

type Decoders added in v1.1.0

type Decoders map[string]Decoder

Decoders is a map that mapping from content type to decoder.

func New added in v1.2.0

func New() *Decoders

New returns a decoders.

func (*Decoders) Decode added in v1.1.0

func (d *Decoders) Decode(r *http.Request, v interface{}) error

Decode data from a request into v, v should be a pointer.

func (*Decoders) Register added in v1.1.0

func (d *Decoders) Register(contentType string, decoder Decoder)

Register a decoder for the given content type.

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL