Skip to content

Commit 100fc2e

Browse files
committed
Added ability to bind to json blobs
1 parent 4e10b3f commit 100fc2e

File tree

3 files changed

+71
-45
lines changed

3 files changed

+71
-45
lines changed

binder.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strconv"
1515
"strings"
1616
"time"
17+
"encoding/json"
1718
)
1819

1920
// A Binder translates between string parameters and Go data structures.
@@ -290,7 +291,16 @@ func unbindSlice(output map[string]string, name string, val interface{}) {
290291
}
291292

292293
func bindStruct(params *Params, name string, typ reflect.Type) reflect.Value {
293-
result := reflect.New(typ).Elem()
294+
resultPointer:= reflect.New(typ)
295+
result := resultPointer.Elem()
296+
if params.JsonRequest {
297+
// Try to inject the response as a json into the created result
298+
if err := json.Unmarshal(params.Json, resultPointer.Interface());err!=nil {
299+
WARN.Println("W: bindStruct: Unable to unmarshal request:", name,err)
300+
}
301+
return result
302+
303+
}
294304
fieldValues := make(map[string]reflect.Value)
295305
for key := range params.Values {
296306
if !strings.HasPrefix(key, name+".") {

params.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/url"
1010
"os"
1111
"reflect"
12+
"io/ioutil"
1213
)
1314

1415
// Params provides a unified view of the request params.
@@ -31,6 +32,8 @@ type Params struct {
3132

3233
Files map[string][]*multipart.FileHeader // Files uploaded in a multipart form
3334
tmpFiles []*os.File // Temp files used during the request.
35+
Json []byte
36+
JsonRequest bool
3437
}
3538

3639
// ParseParams parses the `http.Request` params into `revel.Controller.Params`
@@ -54,6 +57,13 @@ func ParseParams(params *Params, req *Request) {
5457
params.Form = mp.GetValue()
5558
params.Files = mp.GetFile()
5659
}
60+
case "application/json":
61+
fallthrough
62+
case "text/json":
63+
content, _ := ioutil.ReadAll(req.GetBody())
64+
// We wont bind it until we determine what we are binding too
65+
params.Json = content
66+
params.JsonRequest = true
5767
}
5868

5969
params.Values = params.calcValues()

validation.go

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -185,50 +185,56 @@ func (v *Validation) Check(obj interface{}, checks ...Validator) *ValidationResu
185185

186186
// ValidationFilter revel Filter function to be hooked into the filter chain.
187187
func ValidationFilter(c *Controller, fc []Filter) {
188-
errors, err := restoreValidationErrors(c.Request)
189-
c.Validation = &Validation{
190-
Errors: errors,
191-
keep: false,
192-
}
193-
hasCookie := (err != http.ErrNoCookie)
194-
195-
fc[0](c, fc[1:])
196-
197-
// Add Validation errors to ViewArgs.
198-
c.ViewArgs["errors"] = c.Validation.ErrorMap()
199-
200-
// Store the Validation errors
201-
var errorsValue string
202-
if c.Validation.keep {
203-
for _, error := range c.Validation.Errors {
204-
if error.Message != "" {
205-
errorsValue += "\x00" + error.Key + ":" + error.Message + "\x00"
206-
}
207-
}
208-
}
209-
210-
// When there are errors from Validation and Keep() has been called, store the
211-
// values in a cookie. If there previously was a cookie but no errors, remove
212-
// the cookie.
213-
if errorsValue != "" {
214-
c.SetCookie(&http.Cookie{
215-
Name: CookiePrefix + "_ERRORS",
216-
Value: url.QueryEscape(errorsValue),
217-
Domain: CookieDomain,
218-
Path: "/",
219-
HttpOnly: true,
220-
Secure: CookieSecure,
221-
})
222-
} else if hasCookie {
223-
c.SetCookie(&http.Cookie{
224-
Name: CookiePrefix + "_ERRORS",
225-
MaxAge: -1,
226-
Domain: CookieDomain,
227-
Path: "/",
228-
HttpOnly: true,
229-
Secure: CookieSecure,
230-
})
231-
}
188+
// Ignore cookies on json requests
189+
if c.Params !=nil && c.Params.JsonRequest {
190+
c.Validation = &Validation{}
191+
fc[0](c, fc[1:])
192+
} else {
193+
errors, err := restoreValidationErrors(c.Request)
194+
c.Validation = &Validation{
195+
Errors: errors,
196+
keep: false,
197+
}
198+
hasCookie := (err != http.ErrNoCookie)
199+
200+
fc[0](c, fc[1:])
201+
202+
// Add Validation errors to ViewArgs.
203+
c.ViewArgs["errors"] = c.Validation.ErrorMap()
204+
205+
// Store the Validation errors
206+
var errorsValue string
207+
if c.Validation.keep {
208+
for _, error := range c.Validation.Errors {
209+
if error.Message != "" {
210+
errorsValue += "\x00" + error.Key + ":" + error.Message + "\x00"
211+
}
212+
}
213+
}
214+
215+
// When there are errors from Validation and Keep() has been called, store the
216+
// values in a cookie. If there previously was a cookie but no errors, remove
217+
// the cookie.
218+
if errorsValue != "" {
219+
c.SetCookie(&http.Cookie{
220+
Name: CookiePrefix + "_ERRORS",
221+
Value: url.QueryEscape(errorsValue),
222+
Domain: CookieDomain,
223+
Path: "/",
224+
HttpOnly: true,
225+
Secure: CookieSecure,
226+
})
227+
} else if hasCookie {
228+
c.SetCookie(&http.Cookie{
229+
Name: CookiePrefix + "_ERRORS",
230+
MaxAge: -1,
231+
Domain: CookieDomain,
232+
Path: "/",
233+
HttpOnly: true,
234+
Secure: CookieSecure,
235+
})
236+
}
237+
}
232238
}
233239

234240
// Restore Validation.Errors from a request.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy