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
- HTTPRequest and HTTPResponse work across URLSession and SwiftNIO without type conversion
- Built for HTTP/3 and HTTP/2 first, backward compatible with HTTP/1.1
- Type-safe header field access with autocomplete for common and custom headers
- Already integrates with URLSession, SwiftNIO support coming in swift-nio-extras
Apple created a shared HTTP type system that eliminates duplicate abstractions and enables code sharing between iOS apps and Swift servers.
About This Article
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.
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.
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.