Is there any function or a way to work around the RequestContext in PlayFramework, that works similarly to Pekko’s extractRequestContext?
I’m migrating an old Akka rest API to Play3.0, and I need to port this Endpoint to play. Fortunately, Pekko has the exact same function “extractRequestContext”, and the same type I need: StrictForm.FileData
The reason why I want to access the request context is because this API is using a library that needs files to be specifically of the type StrictForm.FileData, so Using a normal request.body.file(“icon”) is not really an option, unless I can convert those files to FileData.
post {
extractRequestContext { _ =>
formFields(
"name".as[String],
"description".as[String].?,
"icon".as[FileData].?,
"color".as[String]
) { (
name: String,
description: Option[String],
icon: Option[FileData],
color: String,
) => {
onboardingController.saveData(
workspaceRequest(userKey, name, description, icon, color)
)
}
}
}
}
I tried to do this while using a Form, but as I said, I could not find a way to convert the file. I know the getOrElse would make it fail in case there is no file, it was just for testing purposes.
I could not find any way to access the requestContext and looking at an old PlayFramework docs, I could not find anything similar
def createWorkspace = Action(parse.multipartFormData) { implicit request =>
request.body.file("icon").map { picture =>
//I wanted to cast the file to StrictForm.FileData, but I did not found a way to do it.
// handle the other form data
workspaceForm.bindFromRequest.fold(
errors =>
println(errors.toString())
BadRequest("Ooops"),
workspaceRequest => {
// this does not work since it returns a Part
// val withPicture = workspaceRequest.copy(icon = Some(picture))
Ok("")
}
)
}.getOrElse(BadRequest("Missing picture."))
}