A Swift library containing constructs for interacting with APIs over the HTTP protocol

What's New


  • Added convenience methods to URLRequest for setting httpBody


swift 5.1 iOS macOS tvOS watchOS Build Status codecov

A Swift package containing constructs for interacting with APIs over the HTTP protocol.


URLSession is great, but it has some small shortcomings. Specifically, when it comes to using RESTful APIs over HTTP. For example, URLRequest has a httpMethod property of type String. I have found myself making a HTTPMethod enum so many times, I decided to take these abstractions around http conventions, and put them in a framework.


Swift Package Manager

You can use the Swift Package Manager to install HTTPKit by adding the proper description to your Package.swift file:

// swift-tools-version:5.1
import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    dependencies: [
        .package(url: "", from: "1.4"),

Then run swift build whenever you get prepared.


Clone the repo with git clone, and drag the Sources/HTTPKit directory into your project


The HTTP.Method type is an enum that you can use directly with URLRequest, like this:

request.method = .get


The HTTP.Header type is an enum giving you type safe HTTP headers, like this:


If there are headers that you need supported, open a an issue, or contribute and add them!


The HTTP.ResponseStatus type is also an enum that gives you strong typing of status codes, so you don't need to worry about remembering what the codes mean. You can get a HTTP.ResponseStatus directly from a URLResponse, like this:

if case .created = response?.status {

There is also mapping of HTTP.Method to HTTP.Response, so you don't need to worry about making sure the response is correct for the method used in the request, like this:

if response.hasValidResponseStatus(for: response.originalRequest) { ...


HTTPNetworkController is a protocol which sends a URLRequest, and decodes the returned json into the supplied generic type. The nice thing about this protocol is that it contains the logic for mapping the response code to the request method, validating the response, and decoding the json. When using this protocol, you don't even need to worry about HTTP.ResponseStatus.

final class UsersController: HTTPNetworkController {

    func getUser(for post: Post, completion: @escaping (Result<User, Error>) -> Void) {
        let request: URLRequest = UsersRequestBuilder().user(for: post).build()
        self.sendRequest(request, completion)



HTTPModelController is a protocol that contains basic CRUD methods for generic types, that inherits from HTTPNetworkController. This sets the HTTP.Method of the URLRequest, and creates and sends the URLRequest.

final class PostsController: HTTPModelController {

    func uploadPost(_ post: Post, completion: @escaping (Result<Void, Error>) -> Void) {
        let requestBuilder: PostsRequestBuilder().create(post)
            with: requestBuilder,
            completion: { (result: Result<Post, Error>) in
                switch result {
                case .success:
                case .failure:



HTTP.RequestBuilder is a class which uses the builder pattern to build your URLRequests. There are examples of this class being used in the example project. Ideally you would make a subclass, and use it like this:

final class PostRequestBuilder: HTTP.RequestBuilder {

    func posts(for user: User) -> PostsRequestBuilder  {
        let result: PostsRequestBuilder = self
        result.appendQueryItem(name: "userId", value: String(describing:
        let authToken: String = ...
        return result


let request: URLRequest = PostRequestBuilder().posts(for: user).build()


If you would like to contribute, fork and submit a PR! Or open Issues! I would love input from the community to make this project the best it can be.


  • Swift Tools 5.1.0
View More Packages from this Author


  • None
Last updated: Sun Mar 12 2023 22:37:00 GMT-0500 (GMT-05:00)