:wave: Multipart question - Is there a way to vie...
# http4k
m
👋 Multipart question - Is there a way to view/extract the body in the
security
but still be able to extract it again in my actual end point? Seems to work fine with a normal string json body but not multipart (which to be fair I wasnt expecting it to work with json). Example below of what im trying to do:
Copy code
fun postImportFile(auth: TenantAuthentication, importManagementService: ImportManagementService): ContractRoute =
    "/tenants" / Path.tenantId() / "import" meta {
        security = auth.isUserInTenant { request ->
            listOf(
                when (postImportMultipart(importFormBody(request)).importType) {
                   // some stuff
                }
            )
        }
        receiving(importFormBody)
        preFlightExtraction = IgnoreBody
    } bindContract <http://Method.POST|Method.POST> to
            { tenantId, _ ->
                { request: Request ->
     
                        val form = importFormBody(request)
                        val importFile = inputFile(form)
                        val additional = additionalFilesMultipart(form)
                        val r = postImportMultipart(form)
                       // do some stuff                    
                }
            }

val inputFile = MultipartFormFile.required("inputFile")
val additionalFilesMultipart = MultipartFormFile.multi.optional("additionalFiles")
val postImportMultipart = MultipartFormField.auto<PostImportFileRequestDTO>(CustomJackson()).required("import")

val importFormBody = Body.multipartForm(Validator.Strict, inputFile, postImportMultipart, additionalFilesMultipart).toLens()
d
It's a stream request and the multipart thing reads from the stream. You could possibly wrap the entire thing in a filter that changed the StreamBody into an MemoryBody and put it into the preSecurityFilter
m
Ah good idea, i'll have a go. Thanks!