Using Content
In Vapor 3, all content types (JSON, protobuf, URLEncodedForm, Multipart, etc) are treated the same. All you need to parse and serialize content is a Codable class or struct.
For this introduction, we will use JSON as an example. But keep in mind the API is the same for any supported content type.
Request
Let’s take a look at how you would parse the following HTTP request.
POST /login HTTP/1.1Content-Type: application/json{"email": "user@vapor.codes","password": "don't look!"}
Decode Request
First, create a struct or class that represents the data you expect.
import Foundationimport Vaporstruct LoginRequest: Content {var email: Stringvar password: String}
Then simply conform this struct or class to Content.
Now we are ready to decode that HTTP request.
router.post("login") { req -> Future inreturn req.content.decode(LoginRequest.self).map(to: HTTPStatus.self) { loginRequest inprint(loginRequest.email) // user@vapor.codesprint(loginRequest.password) // don't look!return .ok}}
We use .map(to:) here since req.content.decode(_:) returns a future.
Other Request Types
Since the request in the previous example declared JSON as its content type, Vapor knows to use a JSON decoder automatically. This same method would work just as well for the following request.
POST /login HTTP/1.1Content-Type: application/x-www-form-urlencodedemail=user@vapor.codes&don't+look!
!!! tip You can configure which encoders/decoders Vapor uses. Read on to learn more.
Response
Let’s take a look at how you would create the following HTTP response.
HTTP/1.1 200 OKContent-Type: application/json{"name": "Vapor User","email": "user@vapor.codes"}
Encode Response
Just like decoding, first create a struct or class that represents the data that you are expecting.
import Foundationimport Vaporstruct User: Content {var name: Stringvar email: String}
Then just conform this struct or class to Content. Now we are ready to encode that HTTP response.
router.get("user") { req -> User inreturn User(name: "Vapor User",email: "user@vapor.codes")}
Other Response Types
Content will automatically encode as JSON by default. You can always override which content type is used
using the as: parameter.
try res.content.encode(user, as: .formURLEncoded)
You can also change the default media type for any class or struct.
struct User: Content {/// See Content.defaultMediaTypestatic let defaultMediaType: MediaType = .formURLEncoded...}
Configuring Content
Use ContentConfig to register custom encoder/decoders for your application. These custom coders will be used anywhere you do content.encode/content.decode.
/// Create default content configvar contentConfig = ContentConfig.default()/// Create custom JSON encodervar jsonEncoder = JSONEncoder()jsonEncoder.dateEncodingStrategy = .millisecondsSince1970/// Register JSON encoder and content configcontentConfig.use(encoder: jsonEncoder, for: .json)services.register(contentConfig)
