表单、post数据和上传文件可以使用以下上下文的方法检索。
// FormValueDefault returns a single parsed form value by its "name",// including both the URL field's query parameters and the POST or PUT form data.// FormValueDefault按“名称”返回一个解析后的表单值,// 包括URL字段的查询参数和POST或PUT表单数据。//// Returns the "def" if not found.// 如果没有找到,返回“def”。FormValueDefault(name string, def string) string// FormValue returns a single parsed form value by its "name",// including both the URL field's query parameters and the POST or PUT form data.// FormValue按其“名称”返回单个解析后的表单值,// 包括URL字段的查询参数和POST或PUT表单数据。FormValue(name string) string// FormValues returns the parsed form data, including both the URL// field's query parameters and the POST or PUT form data.// FormValues返回解析后的表单数据,包括两个URL// 字段的查询参数和POST或PUT表单数据。//// The default form's memory maximum size is 32MB, it can be changed by the// `iris.WithPostMaxMemory` configurator at// main configuration passed on `app.Run`'s second argument.//默认表单的最大内存大小是32MB,它可以通过/ / `iris.WithPostMaxMemory`。WithPostMaxMemory的配置器//通过app传递主配置。运行的第二个参数。//// NOTE: A check for nil is necessary.//注意:检查nil是必要的。FormValues() map[string][]string// PostValueDefault returns the parsed form data from POST, PATCH,// or PUT body parameters based on a "name".// PostValueDefault从POST、PATCH、// 或根据“名字”输入body参数。//// If not found then "def" is returned instead.// 如果没有找到,则返回“def”。PostValueDefault(name string, def string) string// PostValue returns the parsed form data from POST, PATCH,// or PUT body parameters based on a "name"// PostValue从POST、PATCH、//或根据“名称”输入body参数PostValue(name string) string// PostValueTrim returns the parsed form data from POST, PATCH,// or PUT body parameters based on a "name", without trailing spaces.// PostValueTrim从POST、PATCH、// 或基于“名称”放置主体参数,尾部不带空格。PostValueTrim(name string) string// PostValueInt returns the parsed form data from POST, PATCH,// or PUT body parameters based on a "name", as int.// PostValueInt从POST、PATCH、//或基于“名称”放入主体参数,如int。//// If not found returns -1 and a non-nil error.// 如果没有找到,返回-1和一个非nil错误PostValueInt(name string) (int, error)// PostValueIntDefault returns the parsed form data from POST, PATCH,// or PUT body parameters based on a "name", as int.// PostValueIntDefault返回从POST、PATCH、//或基于“名称”放入主体参数,如int。//// If not found returns or parse errors the "def".// 如果没有找到,则返回或解析“def”错误。PostValueIntDefault(name string, def int) int// PostValueInt64 returns the parsed form data from POST, PATCH,// or PUT body parameters based on a "name", as float64.//// If not found returns -1 and a no-nil error.PostValueInt64(name string) (int64, error)// PostValueInt64Default returns the parsed form data from POST, PATCH,// or PUT body parameters based on a "name", as int64.//// If not found or parse errors returns the "def".PostValueInt64Default(name string, def int64) int64// PostValueInt64Default returns the parsed form data from POST, PATCH,// or PUT body parameters based on a "name", as float64.//// If not found returns -1 and a non-nil error.PostValueFloat64(name string) (float64, error)// PostValueInt64Default returns the parsed form data from POST, PATCH,// or PUT body parameters based on a "name", as float64.//// If not found or parse errors returns the "def".PostValueFloat64Default(name string, def float64) float64// PostValueInt64Default returns the parsed form data from POST, PATCH,// or PUT body parameters based on a "name", as bool.//// If not found or value is false, then it returns false, otherwise true.PostValueBool(name string) (bool, error)// PostValues returns all the parsed form data from POST, PATCH,// or PUT body parameters based on a "name" as a string slice.//// The default form's memory maximum size is 32MB, it can be changed by the// `iris.WithPostMaxMemory` configurator at// main configuration passed on `app.Run`'s second argument.PostValues(name string) []string// FormFile returns the first uploaded file that received from the client.//// The default form's memory maximum size is 32MB, it can be changed by the// `iris.WithPostMaxMemory` configurator at// main configuration passed on `app.Run`'s second argument.FormFile(key string) (multipart.File, *multipart.FileHeader, error)
多部分/ Urlencoded形式
func main() {app := iris.Default()app.Post("/form_post", func(ctx iris.Context) {message := ctx.FormValue("message")nick := ctx.FormValueDefault("nick", "anonymous")ctx.JSON(iris.Map{"status": "posted","message": message,"nick": nick,})})app.Listen(":8080")}
另一个例子:查询+发布表单
POST /post?id=1234&page=1 HTTP/1.1Content-Type: application/x-www-form-urlencodedname=manu&message=this_is_great
func main() {app := iris.Default()app.Post("/post", func(ctx iris.Context) {id := ctx.URLParam("id")page := ctx.URLParamDefault("page", "0")name := ctx.FormValue("name")message := ctx.FormValue("message")// or `ctx.PostValue` for POST, PUT & PATCH-only HTTP Methods.app.Logger().Infof("id: %s; page: %s; name: %s; message: %s",id, page, name, message)})app.Listen(":8080")}
id: 1234; page: 1; name: manu; message: this_is_great
上传文件
Iris Context提供了一个上传文件的助手(将文件从请求文件数据保存到主机系统的硬盘)。阅读更多有关上下文的内容。Context.UploadFormFiles下面的方法。
UploadFormFiles上传任何从客户端接收到的文件到系统物理位置“destDirectory”。
第二个可选参数“before”让调用者有机会修改*miltipart。在保存到磁盘之前,它可以用来根据当前请求更改文件的名称,所有FileHeader的选项都可以更改。如果在将文件保存到磁盘之前不需要使用此功能,则可以忽略它。
注意,它不检查请求体是否流化。
如果由于操作系统的权限或net/http导致至少一个新文件无法创建,则返回复制的长度为int64和not nil错误。net/http.ErrMissingFile 如果没有收到文件,错误文件。
如果你想要手动接收和管理文件,你可以使用Context.FormFile。然后创建一个适合您需要的复制函数,下面是通用用法。
默认表单的最大内存大小是32MB,它可以通过iris#WithPostMaxMemory配置器在主配置中对app.Run的第二个参数进行更改。
UploadFormFiles(destDirectory string,before ...func(Context, *multipart.FileHeader)) (n int64, err error)
实例代码:
const maxSize = 5 << 20 // 5MBfunc main() {app := iris.Default()app.Post("/upload", iris.LimitRequestBodySize(maxSize), func(ctx iris.Context) {//// UploadFormFiles// uploads any number of incoming files ("multiple" property on the form input).//// The second, optional, argument// can be used to change a file's name based on the request,// at this example we will showcase how to use it// by prefixing the uploaded file with the current user's ip.ctx.UploadFormFiles("./uploads", beforeSave)})app.Listen(":8080")}func beforeSave(ctx iris.Context, file *multipart.FileHeader) {ip := ctx.RemoteAddr()// make sure you format the ip in a way// that can be used for a file name (simple case):ip = strings.Replace(ip, ".", "_", -1)ip = strings.Replace(ip, ":", "_", -1)// you can use the time.Now, to prefix or suffix the files// based on the current time as well, as an exercise.// i.e unixTime := time.Now().Unix()// prefix the Filename with the $IP-// no need for more actions, internal uploader will use this// name to save the file into the "./uploads" folder.file.Filename = ip + "-" + file.Filename}
怎样用curl:
curl -X POST http://localhost:8080/upload \-F "files[]=@./myfile.zip" \-F "files[]=@./mysecondfile.zip" \-H "Content-Type: multipart/form-data"
更多的例子可以在https://github.com/kataras/iris/tree/master/_examples/request-body和https://github.com/kataras/iris/tree/master/_examples/filesserver找到。
