2

I have a RestFull grails api that i expose through grails Controller. I need to automatically bind the xml data sent in a POST request. I don't think using groovy bindData(object,params) works as the params reference seems not having the xml elements. i use the parseRequest=true in the UrlMappings but yet the params Object does'nt have the xml elements.

am i missing some other config so that my params object get automatically populated with the xml body elements?

tim_yates
  • 167,322
  • 27
  • 342
  • 338
othman
  • 4,428
  • 6
  • 34
  • 45

2 Answers2

7

it seems the right way to do it is to use:

request.reader.text

to get the raw xml. the other methods were not successfull for me.

othman
  • 4,428
  • 6
  • 34
  • 45
1

request.XML is the object holding the Elements parsed from the XML request

Have a look at the request variable available to your controllers.

Grooveek
  • 10,046
  • 1
  • 27
  • 37
  • yes i know about request.XML but it has a serious problem described in link. i can't get request.XML.toString() in a correct format. and the solutions here are not helpfull http://stackoverflow.com/q/7096648/497984 – othman Jun 19 '12 at 14:20
  • do you know a way to get the request.XML string? i tried request.reader.text but this one also caused problems when i send POST request from a remote client. some error like getInputStream() has already been called. – othman Jun 19 '12 at 14:21
  • so, you want the raw XML ? not the parsed one ? – Grooveek Jun 19 '12 at 14:27
  • the raw is better for me as i want to do some Generic object binding work with it. I wrote a binding method that do binding of any xml string without knowing the real structure of the xml. that's why i need the raw xml. a clean way to override `request.XML.toString()` to get the raw xml would be very usefull. – othman Jun 19 '12 at 14:31
  • 1
    if you want the raw XML, try not to import `grails.converters.*` and use the `parseRequest=false` in your mapping. If this still not work, disable xml mime type in config – Grooveek Jun 19 '12 at 14:42
  • I tried all 3 suggestions but yet it still doesn't work! strange why the request.XML.toString() gives this garbage text. Any further help is much appreciated. – othman Jun 19 '12 at 15:07
  • btw i'm using grails 2.0.3. does this version have this annoying request.XML.toString() garbage issue? – othman Jun 19 '12 at 15:16
  • [last chance](http://stackoverflow.com/questions/1046721/accessing-the-raw-body-of-a-put-or-post-request) :-) – Grooveek Jun 19 '12 at 15:26
  • thank you :) but it seems using `request.reader.text` solved the issue. i think this solution is simpler. – othman Jun 19 '12 at 15:42