SkillAgentSearch skills...

Form

:information_source: Decode request body of any types(xml, json, form, multipart form...) into a sctruct by same codebase.

Install / Use

/learn @clevergo/Form
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Form Decoder

Build Status Coverage Status Go.Dev reference Go Report Card Release Downloads Chat Community

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

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)

Form and multipart form are built on top of gorilla schema, tag name is schema.

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

Installation

$ go get clevergo.tech/form

Usage

import (
	"net/http"

	"clevergo.tech/form"
)

var decoders = form.New()

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

func init() {
	// replaces multipart form decoder.
	decoders.Register(form.ContentTypeMultipartForm, form.NewMultipartForm(10*1024*1024))
	// registers other decoder
	// decoders.Register(contentType, decoder)
}

func(w http.ResponseWriter, r *http.Request) {
	u := user{}
	if err := decoders.Decode(r, &u); err != nil {
		http.Error(w, http.StatusInternalServerError, err.Error())
		return
	}
	// ...
}

Example

Checkout example for details.

View on GitHub
GitHub Stars27
CategoryDevelopment
Updated2y ago
Forks3

Languages

Go

Security Score

80/100

Audited on Nov 27, 2023

No findings