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.

  1. POST /login HTTP/1.1
  2. Content-Type: application/json
  3. {
  4. "email": "user@vapor.codes",
  5. "password": "don't look!"
  6. }

Decode Request

First, create a struct or class that represents the data you expect.

  1. import Foundation
  2. import Vapor
  3. struct LoginRequest: Content {
  4. var email: String
  5. var password: String
  6. }

Then simply conform this struct or class to Content. Now we are ready to decode that HTTP request.

  1. router.post("login") { req -> Future in
  2. return req.content.decode(LoginRequest.self).map(to: HTTPStatus.self) { loginRequest in
  3. print(loginRequest.email) // user@vapor.codes
  4. print(loginRequest.password) // don't look!
  5. return .ok
  6. }
  7. }

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.

  1. POST /login HTTP/1.1
  2. Content-Type: application/x-www-form-urlencoded
  3. email=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.

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  3. {
  4. "name": "Vapor User",
  5. "email": "user@vapor.codes"
  6. }

Encode Response

Just like decoding, first create a struct or class that represents the data that you are expecting.

  1. import Foundation
  2. import Vapor
  3. struct User: Content {
  4. var name: String
  5. var email: String
  6. }

Then just conform this struct or class to Content. Now we are ready to encode that HTTP response.

  1. router.get("user") { req -> User in
  2. return User(
  3. name: "Vapor User",
  4. email: "user@vapor.codes"
  5. )
  6. }

Other Response Types

Content will automatically encode as JSON by default. You can always override which content type is used using the as: parameter.

  1. try res.content.encode(user, as: .formURLEncoded)

You can also change the default media type for any class or struct.

  1. struct User: Content {
  2. /// See Content.defaultMediaType
  3. static let defaultMediaType: MediaType = .formURLEncoded
  4. ...
  5. }

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.

  1. /// Create default content config
  2. var contentConfig = ContentConfig.default()
  3. /// Create custom JSON encoder
  4. var jsonEncoder = JSONEncoder()
  5. jsonEncoder.dateEncodingStrategy = .millisecondsSince1970
  6. /// Register JSON encoder and content config
  7. contentConfig.use(encoder: jsonEncoder, for: .json)
  8. services.register(contentConfig)