Hi all, first post.
I got this working but I'm not sure this is a good way of doing this.
I'm processing a POST request on an async middleware's function. The body is encoded as a multipart form. If I use content.decode on the request I get nothing as the body is being streamed. I can collect the body into a ByteStream, make a new request with the collected body and then use content.decode. This works fine but seems a bit cumbersome...
Is there no way to collect the body "in place" and then just use content.decode on the original request? Or maybe set up the middleware so the body is collected before it's called (like in a route)
Here's the code:
let bodyBuffer:ByteBuffer? =
if let buffer = req.body.data {
buffer
} else if let buffer = try? await req.body.collect(upTo: Int.max) {
buffer
} else {
nil
}
guard let bodyBuffer else {
return k_response.emptyOk()
}
let requestWithBody = Request(application: req.application, method: req.method, url: req.url, version: req.version, headers: req.headers, collectedBody: bodyBuffer,on:req.eventLoop)
var info = try requestWithBody.content.decode(MyStruct.self)