Skip to content


When building endpoints, we'll often be returning responses for requests. If we're making outgoing requests, we'll be receiving them.

public let status: Status
public var headers: [HeaderKey: String]
public var body: Body
public var data: Content


The http status associated with the event, for example .ok == 200 ok.


These are the headers associated with the request. If you are preparing an outgoing response, this can be used to add your own keys.

let contentType = response.headers["Content-Type"]  

Or for outgoing response:

let response = response ...
response.headers["Content-Type"] = "application/json"
response.headers["Authorization"] = ... my auth token
Extending Headers

We generally seek to improve code bases by removing stringly typed code where possible. We can add variables to the headers using generic extensions.

extension HTTP.KeyAccessible where Key == HeaderKey, Value == String {
    var customKey: String? {
      get {
        return self["Custom-Key"]
      set {
        self["Custom-Key"] = newValue

With this pattern implemented, our string "Custom-Key" is contained in one section of our code. We can now access like this:

let customKey = response.headers.customKey

// or

let request = ...
response.headers.customKey = "my custom value"


This is the body associated with the response and represents the general data payload. You can view more about body in the associated docs

For responses, the body is most commonly set at initialization. With two main types.


Things that can be converted to bytes, ie:

let response = Response(status: .ok, body: "some string")

In the above example, the String will be automatically converted to a body. Your own types can do this as well.

Bytes Directly

If we already have our bytes array, we can pass it into the body like so:

let response = Response(status: .ok, body: .data(myArrayOfBytes))

To send an HTTP.Response in chunks, we can pass a closure that we'll use to send our response body in parts.

let response = Response(status: .ok) { chunker in
  for name in ["joe", "pam", "cheryl"] {
      try chunker.send(name)

  try chunker.close()

Make sure to call close() before the chunker leaves scope.


We can access content the same we do in a request. This most commonly applies to outgoing requests.

let pokemonResponse = try drop.client.get("")
let names =["results", "name"]?.array


To access JSON directly on a given response, use the following:

let json = request.response["hello"]

Key Paths

For more on KeyPaths, visit here

Serving Files

If you are simply looking to serve files from your public directory, it may be useful to look at 'FileMiddleware' instead.

let res = try Response(filePath: "/path/to/file.txt")

Use this to initialize a file response for the exact file path. If using from a public folder for example, the file name should be appended to the public directory, ie: drop.publicDir + ""

Response(filePath: String, ifNoneMatch: String? = nil, chunkSize: Int = 2048) throws

If none match represents an ETag that will be used to check if the file has changed since the last load by the client. This allows clients like browsers to cache their files and avoid downloading resources unnecessarily. Most often calculated w/

For an example of how this is used, look at 'FileMiddleware'.