Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala play http filters: how to find the request body

I'm trying to write a filter similar to the simple one described in http://www.playframework.com/documentation/2.1.1/ScalaHttpFilters but I need to access the request body. The documentation below states that "when we invoke next, we get back an Iteratee. You could wrap this in an Enumeratee to do some transformations if you wished." I'm trying to figure out how to wrap the Iteratee so I can get the request body as a string within the filter so I can log that as well.

like image 727
Nonos Avatar asked Jul 23 '13 22:07

Nonos


1 Answers

First thing you have to know is when the Filter is invoked, the request body is not parsed yet. That's why it's giving you a RequestHeader. You'll have to find out the type of body, and call the proper body parser accordingly.

You can find a example of body parsing in the CSRF filter (It can lookup for CSRF tokens in the first bytes of the request body).

See: https://github.com/playframework/playframework/blob/master/framework/src/play-filters-helpers/src/main/scala/csrf.scala#L221-L233.

Hope it helps.

like image 173
Julien Tournay Avatar answered Nov 01 '22 00:11

Julien Tournay