我有一些 Controller ( ExampleController
) 接收带有 content-type
的请求application/x-www-form-urlencoded
.
我需要使用 POST
将所有请求数据发送到不同的 URL要求。数据需要与接收到的顺序相同。
问题是内容不匹配因为request.getParameterMap()
破坏数据的顺序。
在 ExampleController
:
def method(){
String s = request.reader.text //this is empty, need a way to read this text
Map<String, String[]> vars = request.getParameterMap() //it's not good for me because the map is unordered map
//but it full of data
}
这是行不通的。
我需要这样的东西:
byte[] data = request.getRequestData()
wr.write(data)
顺便说一句,我试过了:
InputStream = request.getInputStream()
byte [] bytes = inputStream.getBytes()
我也试过
String s = request.reader.text
但是字符串是空的。 我认为主要问题是 grails 机制甚至在 Controller 启动之前读取输入流并将数据放入参数 hashMap 中。有办法撤销吗?
任何帮助将不胜感激
最佳答案
尝试改用 request.reader.text。
def result = request.reader.text.split('&').inject([:]) { map, token ->
token.split('=').with { map[it[0]] = it[1] }
map
}
关于java - Grails 从请求中提取正文数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20544842/