Search code examples
swiftbackendvapor

Vapor How to configure request body size for an individual route


I am building my back-end with Vapor. And I am trying to change the default max body size for one particular route to avoid the 413 Payload Too Large error.

I followed the instructions here and tried to put:

app.on(.POST, "listings", body: .collect(maxSize: "1mb")) { req in
    // Handle request. 
}

in both

func routes(_ app: Application) throws {}

and

public func configure(_ app: Application) throws {}

but they both throw the error Type '()' cannot conform to 'ResponseEncodable'atapp What's the correct way of doing this? Thanks!

EDIT: Thanks to Leo and 0xTim's answer, I was able to resolve the above error. However, I am still having 413 Payload Too Large error with the below code:

func routes(_ app: Application) throws {    
    try app.register(collection: ListingController())
    app.on(.POST, "api/listings", body: .collect(maxSize: "10mb"), use: ListingController.post)
}
struct ListingController: RouteCollection {
    func boot(routes: RoutesBuilder) throws {
        let listingRoutes = routes.grouped("api", "listings")
        listingRoutes.post(use: ListingController.post)
    }

    static func post(request: Request) throws -> EventLoopFuture<List> {
        let data = try req.content.decode(CreateListData.self)
        let list = try List(list: data.list)
        return list(on: req.db).map { list }
    }
}

Solution

  • After trying many times. I finally found the right way of doing it for my situation.

    func boot(routes: RoutesBuilder) throws {
        let listingRoutes = routes.grouped("api", "listings")
        listingRoutes.on(.POST, body: .collect(maxSize: "10mb"), use: postHandler)
    }