form

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2021 License: MIT Imports: 3 Imported by: 0

README

Forms

This encoder and decoder will parse values from and to your http.Request form data.

Usage

func MyHandler(w http.ResponseWriter, r *http.Request) {
	var body requestBody
	if err := form.Decode(r, &body); err != nil {
		w.WriteHeader(http.StatusUnprocessibleEntity)
		return
	}

	...
}

type requestBody struct {
	Name       string `form:"name"`
	Age        int    `form:"age"`
	NewsLetter bool   `form:"news_letter"`
}

Documentation

Overview

Package forms is for parsing http.Request form data

Usage:

func MyHandler(w http.ResponseWriter, r *http.Request) {
    var body reqBody
    if err := form.Decode(r, &body); err != nil {
         w.WriterHeader(http.StatusUnprocessibleEnitity)
         return
    }
}

type reqBody struct {
    Name string `form:"name"`
    Age  int    `form:"age"`
}

Index

Constants

This section is empty.

Variables

View Source
var ErrNoHeaders = fmt.Errorf("error request doesnt contain the Content-Type: '%s'", contentTypeVal)

ErrNoHeaders is returned if the request doesnt contain the right content type for parsing the form

Functions

func Marshal added in v1.3.1

func Marshal(r *http.Request, obj interface{}) error

Marshal writes the given data to the the form and adds the Content-Type header to the request

func Unmarshal added in v1.3.1

func Unmarshal(r *http.Request, obj interface{}) error

Umarshal parses the form values into the given object or returns an error if the Content-Type header is not set or if the parsing fails

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder can decode any forms into a given object

func NewDecoder

func NewDecoder(r *http.Request) *Decoder

NewDecoder returns a new Decoder

func (Decoder) Decode

func (x Decoder) Decode(obj interface{}) error

Decode parses the form values into the given object or returns an error if the Content-Type header is not set or if the parsing fails

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder will write form values into a http.Request

func NewEncoder

func NewEncoder(r *http.Request) *Encoder

NewEncoder returns a new Encoder and sets the content type header to the given request

func (Encoder) Encode

func (x Encoder) Encode(obj interface{}) error

Encode writes the values of the given object into the request form

Jump to

Keyboard shortcuts

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