6

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

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
royB
  • 12,779
  • 15
  • 58
  • 80

1 Answers1

1

Try using request.reader.text instead.

def result = request.reader.text.split('&').inject([:]) { map, token ->
  token.split('=').with { map[it[0]] = it[1] }
  map
}
Joshua Moore
  • 24,706
  • 6
  • 50
  • 73
  • Thanks for the response. request.reader.text is an empty String (""). All the data is under request.parameters HashMap – royB Dec 12 '13 at 13:59
  • You have to prevent the request from being parsed first. Supposedly, when using urlMappings you can set parseRequest to false. Then you can read the request body yourself: "/controller/action"(parseRequest:false). However in the latest version of grails, I can't get this to work yet. – user2782001 Nov 10 '19 at 03:26