httpform

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2024 License: MIT Imports: 4 Imported by: 0

README

Wrapper for net/http

Usage

Example data
--boundary
Content-Disposition: form-data; name="name"

mazrean
--boundary
Content-Disposition: form-data; name="password"

password
--boundary
Content-Disposition: form-data; name="icon"; filename="icon.png"
Content-Type: image/png

icon contents
--boundary--
func createUserHandler(res http.ResponseWriter, req *http.Request) {
	parser, err := httpform.NewParser(req)
	if err != nil {
		res.WriteHeader(http.StatusBadRequest)
		return
	}

	err = parser.Register("icon", func(r io.Reader, header formstream.Header) error {
		name, _, _ := parser.Value("name")
		password, _, _ := parser.Value("password")

		return saveUser(req.Context(), name, password, r)
	}, formstream.WithRequiredPart("name"), formstream.WithRequiredPart("password"))
	if err != nil {
		log.Printf("failed to register: %s\n", err)
		res.WriteHeader(http.StatusInternalServerError)
		return
	}

	err = parser.Parse()
	if err != nil {
		res.WriteHeader(http.StatusBadRequest)
		return
	}

	res.WriteHeader(http.StatusCreated)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

type Parser struct {
	*formstream.Parser
	// contains filtered or unexported fields
}

func NewParser

func NewParser(req *http.Request, options ...formstream.ParserOption) (*Parser, error)

func (*Parser) Parse

func (p *Parser) Parse() error

Jump to

Keyboard shortcuts

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