Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Params object (include request.body) #488

Closed
TeemoWan opened this issue Jan 28, 2014 · 5 comments
Closed

Support for Params object (include request.body) #488

TeemoWan opened this issue Jan 28, 2014 · 5 comments
Labels
Milestone

Comments

@TeemoWan
Copy link

Now, All request parameters are collected into a single Params object. That includes:

URL Path parameters
URL Query parameters
Form values (Multipart or not)
File uploads

but request body parameters are not collected into a single Params object. examaple: angluar's json request.

my english is poor, hope you understand,sorry

@brendensoares
Copy link
Member

I think I understand what you are asking, but can you link to a reference example? Thanks!

@verdverm
Copy link
Contributor

verdverm commented Feb 6, 2014

the Params object gets filled from the Request object in this function:
https://github.com/robfig/revel/blob/master/params.go#L32

the Body field of the Request object is an io.ReadCloser
http://golang.org/pkg/net/http/#Request

this is probably why it is not exposed through the Params object.

You can get to the Body field through
c.Request.Body

you will have to close the ReadCloser once you finished reading from it

@becomingbabyman
Copy link

@wanli2011 I ran into same issue with AngularJS and content-type: application/json. If you only need support for the top level key/value pairs in a json object this filter should do the trick.

var JsonParamsFilter = func(c *revel.Controller, fc []revel.Filter) {
    if strings.Contains(c.Request.ContentType, "application/json") {
        data := map[string]string{}
        content, _ := ioutil.ReadAll(c.Request.Body)
        json.Unmarshal(content, &data)
        for k, v := range data {
            c.Params.Values.Set(k, v)
        }
    }
    fc[0](c, fc[1:]) // Execute the next filter stage.
}

Insert it right after the revel.ParamsFilter

revel.ParamsFilter,  // Parse parameters into Controller.Params.
JsonParamsFilter,    // Monkey patch JSON params

@rendon
Copy link

rendon commented Apr 15, 2015

@smothers 👍 After a while trying to read some JSON from the request body this line did the trick.

content, _ := ioutil.ReadAll(c.Request.Body)

@notzippy
Copy link
Collaborator

JSON is now collected as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants