Question

I have a Play 2.0 web application written in Scala. I have generated a WAR file using the play war plugin and deployed on tomcat 7.0.27.

When I submit a form with enctype="multipart/form-data", the request falls over. The error seems to be with the play framework not finding a 'boundary' to the data.

I have already tried putting allowCasualMultipartParsing="true" but it doesn't help

The same code works fine on play server. What am I missing on Tomcat? Is there a specific configuration that enables/ suppresses multipart?

The relevant form code is:

<form method="POST" action="/create" id="myform" class="form-horizontal" enctype="multipart/form-data"> 
<input type="file" name="logo" id="logo"  placeholder="Logo Image" class="input-xlarge" />
<input type="submit" value="Save Details" class="btn btn-info"/>
</form>

The controller code looks like:

val hash_string = "xxxxxxxxxxxx"
var logoFile: String = null

request.body.asMultipartFormData.map { x => x.file("logo").map { logo => logoFile = uploadFile(hash_string, logo) } }

even if I do not upload a file, I still get a BadRequest - dump as follows:

Map(HOST -> non-empty iterator, ACCEPT-ENCODING -> non-empty iterator, 
ACCEPT-LANGUAGE -> non-empty iterator, REFERER -> non-empty iterator, 
CONTENT-LENGTH -> non-empty iterator, ORIGIN -> non-empty iterator, 
ACCEPT-CHARSET -> non-empty iterator, CONNECTION -> non-empty 
iterator, CONTENT-TYPE -> empty iterator, CACHE-CONTROL -> non-empty 
iterator, ACCEPT -> non-empty iterator, COOKIE -> empty iterator, USER- 
AGENT -> non-empty iterator) Map(utmc -> Cookie(utmc, 
84437902,-1,/,None,false,false), utmz -> Cookie(utmz, 
84437902.1337318013.1.1.utmcsr,-1,/,None,false,false), PLAYSESSION -> 
Cookie(PLAY_SESSION,11cd5ffd7e2b56910a6a118e0de60be50c963697-email 
%3Axxxxxxxxxxx,-1,/,None,false,false), __utma -> Cookie(utma, 
84437902.1740292094.1337318013.1337510573.1337583859.6,-1,/,None,false,fals e), 
__utmb -> Cookie(_utmb, 
84437902.2.10.1337583859,-1,/,None,false,false))' [Missing boundary 
header] 
Was it helpful?

Solution

The issue was that if you use "request.body.asMultipartFormData" as I was using in my code, it works with play but does not on tomcat. However, if you use the 'parse.multipartFormData' BodyParser, that works. Don't know why, but it does so now, my method looks like:

def create = IsMultipartAuthenticated(parse.multipartFormData) { user 
  => implicit request => 
  ... 
  request.body.file("logo").map { logo => ... } 
  ... 
  Ok 
} 

I have added a method to Secured trait:

def IsMultipartAuthenticated(p: BodyParser[MultipartFormData[TemporaryFile]])(f: => String => 
  Request[MultipartFormData[TemporaryFile]] => Result) = 
  Security.Authenticated(username, onUnauthorized) { user => 
    Action(p)(request => f(user)(request)) 
  } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top