Content

public protocol Content : Decodable, Encodable, RequestDecodable, RequestEncodable, ResponseDecodable, ResponseEncodable

Convertible to / from content in an HTTP message.

Conformance to this protocol consists of:

If adding conformance in an extension, you must ensure the type already exists to Codable.

struct Hello: Content {
    let message = "Hello!"
}

router.get("greeting") { req in
    return Hello() // {"message":"Hello!"}
}
  • defaultContentType Default implementation

    The default MediaType to use when encoding content. This can always be overridden at the encode call.

    Default implementation is MediaType.json for all types.

    struct Hello: Content {
        static let defaultContentType = .urlEncodedForm
        let message = "Hello!"
    }
    
    router.get("greeting") { req in
        return Hello() // message=Hello!
    }
    
    router.get("greeting2") { req in
        let res = req.response()
        try res.content.encode(Hello(), as: .json)
        return res // {"message":"Hello!"}
    }
    

    Default Implementation

    Default implementation is MediaType.json for all types.

    See Content.

    Declaration

    Swift

    static var defaultContentType: MediaType { get }