From f8f6abd9061483cdc18c8e0816b7a56408ec7a17 Mon Sep 17 00:00:00 2001 From: Gerasimos Maropoulos Date: Thu, 28 Jul 2016 12:13:54 +0300 Subject: [PATCH] Add PostValuesAll, PostValues, PostValue shortcut for aio multipart, form data, raw post, query data --- context.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++ context/context.go | 3 +++ 2 files changed, 64 insertions(+) diff --git a/context.go b/context.go index 82afc7cb..fd793167 100644 --- a/context.go +++ b/context.go @@ -317,6 +317,67 @@ func (ctx *Context) FormValues(name string) []string { return arrStr } +// PostValuesAll returns all post data values with their keys +// multipart, form data, get & post query arguments +func (ctx *Context) PostValuesAll() (valuesAll map[string][]string) { + reqCtx := ctx.RequestCtx + valuesAll = make(map[string][]string) + // first check if we have multipart form + multipartForm, err := reqCtx.MultipartForm() + if err == nil { + //we have multipart form + return multipartForm.Value + } + // if no multipart and post arguments ( means normal form) + + if reqCtx.PostArgs().Len() == 0 && reqCtx.QueryArgs().Len() == 0 { + return // no found + } + + reqCtx.PostArgs().VisitAll(func(k []byte, v []byte) { + key := string(k) + value := string(v) + // for slices + if valuesAll[key] != nil { + valuesAll[key] = append(valuesAll[key], value) + } else { + valuesAll[key] = []string{value} + } + + }) + + reqCtx.QueryArgs().VisitAll(func(k []byte, v []byte) { + key := string(k) + value := string(v) + // for slices + if valuesAll[key] != nil { + valuesAll[key] = append(valuesAll[key], value) + } else { + valuesAll[key] = []string{value} + } + }) + + return +} + +// PostValues returns the post data values as []string of a single key/name +func (ctx *Context) PostValues(name string) []string { + values := make([]string, 0) + if v := ctx.PostValuesAll(); v != nil && len(v) > 0 { + values = v[name] + } + return values +} + +// PostValue returns the post data value of a single key/name +// returns an empty string if nothing found +func (ctx *Context) PostValue(name string) string { + if v := ctx.PostValues(name); len(v) > 0 { + return v[0] + } + return "" +} + // Subdomain returns the subdomain (string) of this request, if any func (ctx *Context) Subdomain() (subdomain string) { host := ctx.HostString() diff --git a/context/context.go b/context/context.go index 17a49f24..f02864f1 100644 --- a/context/context.go +++ b/context/context.go @@ -43,6 +43,9 @@ type ( RequestHeader(k string) string FormValueString(string) string FormValues(string) []string + PostValuesAll() map[string][]string + PostValues(name string) []string + PostValue(name string) string SetStatusCode(int) SetContentType(string) SetHeader(string, string)