Swiftpack.co - swift-server/http as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by swift-server.
swift-server/http 0.1.0
:warning: Historical HTTP API - please use https://github.com/swift-server/async-http-client instead
⭐️ 711
🕓 4 years ago
.package(url: "https://github.com/swift-server/http.git", from: "0.1.0")

Swift Server Project HTTP APIs

:warning: This project is unmaintained experimental legacy code. It has been obsoleted by SwiftNIO which contains the recommended HTTP API of the Swift Server Work Group.

It remains here for historical interest only.

Getting Started

Hello World

The following code implements a very simple "Hello World!" server:

import Foundation
import HTTP

func hello(request: HTTPRequest, response: HTTPResponseWriter ) -> HTTPBodyProcessing { 
    response.writeHeader(status: .ok) 
    response.writeBody("Hello, World!") 
    return .discardBody 

let server = HTTPServer()
try! server.start(port: 8080, handler: hello)


The hello() function receives a HTTPRequest that describes the request and a HTTPResponseWriter used to write a response.

Data that is received as part of the request body is made available to the closure that is returned by the hello() function. In the "Hello World!" example the request body is not used, so .discardBody is returned.

Echo Server

The following code implements a very simple Echo server that responds with the contents of the incoming request:

import Foundation
import HTTP

func echo(request: HTTPRequest, response: HTTPResponseWriter ) -> HTTPBodyProcessing {
    response.writeHeader(status: .ok)
    return .processBody { (chunk, stop) in
        switch chunk {
        case .chunk(let data, let finishedProcessing):
            response.writeBody(data) { _ in
        case .end:
            stop = true

let server = HTTPServer()
try! server.start(port: 8080, handler: echo)


As the Echo server needs to process the request body data and return it in the reponse, the echo() function returns a .processBody closure. This closure is called with .chunk when data is available for processing from the request, and .end when no more data is available.

Once any data chunk has been processed, finishedProcessing() should be called to signify that it has been handled.

When the response is complete, response.done() should be called.

API Documentation

Full Jazzy documentation of the API is available here:



We are actively seeking feedback on this prototype and your comments are extremely valuable. If you have any comments on the API design, the implementation, or any other aspects of this project, please email the swift-server-dev mailing list.

Writing Code

We also welcome code contributions. If you are developing on macOS, you may want to work within Xcode. This project uses the Swift Package Manager. To work on this project within Xcode you can run the Swift Package Manager command swift package generate-xcodeproj to generate an .xcodeproj to work on within Xcode.


This project is based on an inital proposal from @weissi on the swift-server-dev mailing list:


Stars: 711
Last commit: 4 years ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

HTTP 0.1.0
5 years ago

Provides a basic HTTP server that responds to incoming requests.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics