Question

I am running scalatra 2.2 using sbt version 0.13.0 and using its in-built server(jetty). I am running on kubuntu 13.10 64 bit and my java version 1.7.0_51. I am using OpenJDK 64-Bit. In my sample app i can get GET parameters through params but i cannot get POST parameters. I checked the request.body and it shows i am sending those parameters. My code for GET looks like this

get("/requests") {
    val typ = params.getOrElse("type", null)
    Map(
        "success" -> true,
        "respones" -> responses.get(typ)
      )
  }

This works.

My code for POST looks like this

post("/user/register") {
   val typ = params.getOrElse("type", null) // here typ is always null even when i send type as formdata
   if (typ == null) {
       halt(400, Map("type"-> typ, "body"-> request.body.toString))
   }
   // more code
}

This does not work. The response i get is

{"type":null,"body":"------WebKitFormBoundaryE05sRkwjtB4gBgSW\r\nContent-Disposition: form-data; name=\"type\"\r\n\r\nzombie\r\n------WebKitFormBoundaryE05sRkwjtB4gBgSW--\r\n"}

So you see is that the parameter type is there in request body but null in params. I also tried this but i was unable to get POST requests to work. There is a github issue here Help?

Was it helpful?

Solution

If you include the FileUploadSupport trait it will allow you to process multipart forms as your example shows a form boundary suggesting this is the issue.

An example inclusion:

import org.scalatra.ScalatraServlet
import org.scalatra.servlet.{FileUploadSupport, MultipartConfig, SizeConstraintExceededException}

class MyApp extends ScalatraServlet with FileUploadSupport {
  configureMultipartHandling(MultipartConfig(maxFileSize = Some(3*1024*1024)))
  // ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top