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

我有一个第三方应用程序发布数据的控制器操作,问题是POST请求参数为空,这是由于POST Content-Type ='text'。 如果我使用Content-Type ='application / x-www-form-urlencoded'模拟(使用Chrome Rest Console插件)相同的POST,则正确填充请求参数。

如何使用Content-Type ='text'启用POST数据?


您可以使用访问原始POST数据

request.reader.text

据我所知(我可能是错的),grails不会解析这个内容类型,因为它最终使用HTTPServletRequest ,它只分析www-form-urlencoded ,因为这是通过HTTP传递变量值的正常方式。

您可以使用grails过滤器轻松地模拟您所需的行为,例如:

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
                    }
                }
            }
        }
    }
}

笔记:

您可能想要从此过滤器中排除资产请求等。

内容类型应该是text/plain ,因为这是开箱即用的文本格式,但我相信您可以在Config.groovy进行设置。

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

上一篇: Grails Form POST with missing params when form Content

下一篇: Android set content type HttpPost