14

I have a scalatra servlet:

post("/asdf") {
  ???
}

And my clients send xml in post body, so I need to extract raw text from request. How do I do it in scalatra?

2 Answers 2

20
request.body

gives you access to the request body. So if it is XML and you want it as a NodeSeq, do:

XML.loadString(request.body)
1
5

+1, good question

You have access to Servlet Request via "request" keyword within a Scalatra route, so getInputStream and getContentLength provide access if the post body itself is the xml string; i.e. client is not passing xml stored in named field as part of a form post. If the latter, then the below should do the trick:

post("/foo" && request.getHeader("Accept-Encoding") contains "application/xml") {
  val xml = XML.fromString(params("xml-param-field-name"))
}

If you want to use above parse from string, see Anti-XML Integration in the Scalatra Book

2
  • am new to scala and doing this ^^ am getting compilation error on &&/contains. Commented Apr 28, 2017 at 13:33
  • @gursahib.singh.sahni not sure, it's been 5 years since I used Scalatra ;-) Maybe try their Gitter channel Commented Apr 29, 2017 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.