@@ -4,18 +4,12 @@ import (
44 "io"
55 "net/http"
66 "net/url"
7+ "strconv"
78)
89
9- // BodyAs(obj) err // request body as specified class (deserialized from JSON)
10- // QueryAs(obj) err // request body as specified class (deserialized from JSON)
11-
12- // Request methods
13- // body() // request body as string
14- // bodyAsBytes() // request body as array of bytes
15-
1610// BodyBytes get body as array of bytes
1711func (ctx * Context ) BodyBytes () (body []byte , err error ) {
18- if cb := ctx .Get (BodyBytesKey ); cb != nil {
12+ if cb , exist := ctx .Get (BodyBytesKey ); exist && cb != nil {
1913 if cbb , ok := cb .([]byte ); ok {
2014 body = cbb
2115 }
@@ -32,51 +26,47 @@ func (ctx *Context) BodyBytes() (body []byte, err error) {
3226 return
3327}
3428
35- // bodyStreamAsClass(clazz) // request body as specified class (memory optimized version of above)
36- // bodyValidator(clazz) // request body as validator typed as specified class
37- // bodyInputStream() // the underyling input stream of the request
38-
39- // formParam("name") // form parameter by name, as string
40- // formParamAsClass("name", clazz) // f orm parameter by name, as validator typed as specified class
41- // formParams("name") // list of form parameters by name
42- // formParamMap() // map of all form parameters
43-
44- // pathParam("name") // path parameter by name as string
45- // pathParamAsClass("name", clazz) // path parameter as validator typed as specified class
46- // pathParamMap() // map of all path parameters
47-
48- // queryParam("name") // query param by name as string
49- // queryParamAsClass("name", clazz) // query param parameter by name, as validator typed as specified class
50- // queryParams("name") // list of query parameters by name
51- // queryParamMap() // map of all query parameters
52- // queryString() // full query string
53-
54- // uploadedFile("name") // uploaded file by name
55- // uploadedFiles("name") // all uploaded files by name
56- // uploadedFiles() // all uploaded files as list
57- // uploadedFileMap() // all uploaded files as a "names by files" map
58-
59- // basicAuthCredentials() // basic auth credentials (or null if not set)
60-
61- // attribute("name", value) // set an attribute on the request
62- // attribute("name") // get an attribute on the request
63- // attributeOrCompute("name", ctx -> {}) // get an attribute or compute it based on the context if absent
64- // attributeMap() // map of all attributes on the request
29+ // GetParam returns the value of the first Param which key matches the given name.
30+ // If no matching Param is found, an empty string is returned.
31+ func (ctx * Context ) GetParam (name string ) string {
32+ for i := 0 ; i < ctx .paramCount ; i ++ {
33+ if ctx .paramNames [i ] == name {
34+ return ctx .paramValues [i ]
35+ }
36+ }
37+ return ""
38+ }
6539
66- // contentLength() // content length of the request body
67- // contentType() // request content type
40+ // GetParamByIndex get one parameter per index
41+ func (ctx * Context ) GetParamByIndex (index int ) string {
42+ return ctx .paramValues [index ]
43+ }
6844
69- // isMultipart() // true if the request is multipart
70- // isMultipartFormData() // true if the request is multipart/formdata
45+ // @TODO: cache
46+ func (ctx * Context ) QueryParam (name string , defaultValue ... string ) string {
47+ if val := ctx .Request .URL .Query ().Get (name ); val != "" {
48+ return val
49+ }
50+ for _ , v := range defaultValue {
51+ return v
52+ }
53+ return ""
54+ }
7155
72- // sessionAttribute("name", value) // set a session attribute
73- // sessionAttribute("name") // get a session attribute
74- // consumeSessionAttribute("name") // get a session attribute, and set value to null
75- // cachedSessionAttribute("name", value) // set a session attribute, and cache the value as a request attribute
76- // cachedSessionAttribute("name") // get a session attribute, and cache the value as a request attribute
77- // cachedSessionAttributeOrCompute(...) // same as above, but compute and set if value is absent
78- // sessionAttributeMap() // map of all session attributes
79- // cookieMap() // map of all request cookies
56+ func (ctx * Context ) QueryParamInt (name string , defaultValue ... int ) int {
57+ str := ctx .QueryParam (name , "0" )
58+ if str == "" {
59+ for _ , v := range defaultValue {
60+ return v
61+ }
62+ return 0
63+ }
64+ val , err := strconv .Atoi (str )
65+ if err != nil {
66+ return 0
67+ }
68+ return val
69+ }
8070
8171// Host host as string
8272func (ctx * Context ) Host () string {
@@ -118,10 +108,6 @@ func (ctx *Context) GetCookie(name string) *http.Cookie {
118108 return nil
119109}
120110
121- // header("name") // request header by name (can be used with Header.HEADERNAME)
122- // headerAsClass("name", clazz) // request header by name, as validator typed as specified class
123- // headerMap() // map of all request headers
124-
125111// GetHeader gets the first value associated with the given key. If there are no values associated with the key,
126112// GetHeader returns "".
127113// It is case insensitive; textproto.CanonicalMIMEHeaderKey is used to canonicalize the provided key. Get assumes
0 commit comments