reqconv

package
v0.0.0-...-f4188f5 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package reqconv implements auto parsing a HTTP request as golang struct according to its content type. The supported content type are:

  • application/json
  • application/xml
  • multipart/form-data
  • application/x-www-form-urlencoded

For a request without body, e.g., GET, DELETE, HEAD, TRACE, it will parse the URL query into given pointer.

It returns a error when other types incoming.

As of Golang struct, the supported types are:

  • int
  • bool
  • string
  • float64
  • *multipart.FileHeader
  • slice of above

For example, a file upload request:

var params struct {
	Int   int                   `json:"int"`
	Bool  bool                  `json:"bool"`
	File  *multipart.FileHeader `json:"file"`
	Array []string              `json:"array"`
}

params.Int = 233 // default value if any.
err := reqconv.Unmarshal(req, &params)

By default, the form data key uses `json`, same as json for consistency. You can change it by:

form.FieldTag = "form"

If no tag specified, it will use cammel case of the field name since most languages fields start with lower case.

As of xml, however, you must use the `xml` tag.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unmarshal

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

Unmarshal auto parses a HTTP request r into ptr according to its content type.

Types

This section is empty.

Jump to

Keyboard shortcuts

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