Swift.org Jul 10, 2023

Introducing Swift HTTP Types

Article Summary

Apple just open-sourced Swift HTTP Types, and it's about to change how iOS and server-side Swift developers handle HTTP. No more converting between URLRequest and SwiftNIO types.

Apple's Foundation networking team released a new package that provides shared currency types for HTTP operations across client and server Swift code. The goal is to replace SwiftNIO's HTTPRequestHead/HTTPResponseHead and eventually integrate with Foundation's URLRequest/URLResponse, creating a unified HTTP abstraction layer.

Key Takeaways

Critical Insight

Apple created a shared HTTP type system that eliminates duplicate abstractions and enables code sharing between iOS apps and Swift servers.

The team hints at future proposals for standardized body handling that could further unify the Swift HTTP ecosystem.

About This Article

Problem

Swift developers working on both clients and servers had to maintain separate HTTP type systems. URLSession on Apple platforms and SwiftNIO on servers required different abstractions, which meant writing redundant code and handling constant conversions between them.

Solution

Apple's Foundation networking team built HTTPRequest and HTTPResponse as framework-agnostic types that work across Swift environments. These types represent any valid HTTP message and include straightforward APIs for configuring method, scheme, authority, and path.

Impact

Code can now be shared between clients and servers. The package supports HTTP/3, HTTP/2, and HTTP/1.1. Eventually it will replace SwiftNIO's HTTPRequestHead and HTTPResponseHead, along with Foundation's URLRequest and URLResponse for HTTP message handling.