Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails extract body data from the request

I have some controller (ExampleController) that receives requests with content-type application/x-www-form-urlencoded.

I need to send all the request data to a different URL using POST request. The data needs to be in the same order as received it.

Problem is that the content does not match because request.getParameterMap() destroys the order of the data. In 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

}

which does not work.

I need something like:

byte[] data = request.getRequestData()
wr.write(data)

btw i've tried:

InputStream = request.getInputStream()
byte [] bytes = inputStream.getBytes()

I've also tried

String s = request.reader.text

but the string is empty. I think the main problem is that grails mechanism reads the input stream before the controller even starts and place the data in the parameters hashMap. is there a way to undo it?

Any help will be greatly appreciated

like image 838
royB Avatar asked Nov 10 '22 16:11

royB


1 Answers

Try using request.reader.text instead.

def result = request.reader.text.split('&').inject([:]) { map, token ->
  token.split('=').with { map[it[0]] = it[1] }
  map
}
like image 115
Joshua Moore Avatar answered Nov 14 '22 21:11

Joshua Moore