Grails Form POST with missing params when form Content

I've a controller action where a third party application posts data, the problem is that the POST request params are empty, this is due to POST Content-Type = 'text'. If I simulate (with Chrome Rest Console plugin) the same POST with Content-Type = 'application/x-www-form-urlencoded' then the request params are correctly filled.

How can I enable POST data with Content-Type = 'text'?


You can access the raw POST data using

request.reader.text

As far as I can tell (and I might be wrong) grails won't parse this content type, because it ultimately uses HTTPServletRequest , which only parses www-form-urlencoded , as this is the normal way to pass variable values thorugh HTTP.

You can easily emulate the behaviour you desire by using a grails filter, such as this:

class TextRequestFilters {
    def filters= {
        textRequestFilter(controller: '*', action:'*') {
            before = {
                if(request.post && request.format == 'text') {
                    String postBody = request.reader.text
                    String[] variables = postBody.split('&')
                    variables.each { String variable ->
                        String[] parts = variable.split('=')
                        String key = URLDecoder.decode(parts[0], request.characterEncoding)
                        String value = URLDecoder.decode(parts[1], request.characterEncoding)
                        params[key] = value
                    }
                }
            }
        }
    }
}

Notes:

You probably want to exclude asset requests and such from this filter.

Content type should be text/plain , as this is what grails translates to text format out of the box, but I belive you can set this up in Config.groovy .

链接地址: http://www.djcxy.com/p/50956.html

上一篇: procmail在内容上给予“不匹配”

下一篇: 表单内容时,Grails会形成缺少参数的POST