swift-http-types

1.4.0

Version-independent HTTP currency types for Swift
apple/swift-http-types

What's New

1.4.0

2025-04-04T20:13:35Z

What's Changed

SemVer Minor

  • Enable MemberImportVisibility check on all targets by @rnro in #78

SemVer Patch

  • Request.url should return nil for situations where it is not applicable by @guoye-zhang in #84
  • Eliminate hasPrefix usage for pseudo HTTP header names by @guoye-zhang in #87
  • PseudoHeaderFields adopting CoW to reduce size by @guoye-zhang in #88

Other Changes

  • add .editorconfig file by @rnro in #73
  • Aligning semantic version label check name by @FranzBusch in #75
  • Update release.yml by @FranzBusch in #80
  • Enable strict concurrency by @clintonpi in #82
  • Update benchmark threshold to 6.1 by @guoye-zhang in #83
  • CI use 6.1 nightlies by @rnro in #85
  • Rename nightly_6_1 params to nightly_next by @rnro in #91
  • Only apply standard swift settings on valid targets by @rnro in #93
  • Add static SDK CI workflow by @rnro in #95
  • Enable macOS CI on merge to main and daily timer by @rnro in #96
  • Enable macOS CI on pull requests by @rnro in #97

New Contributors

Full Changelog: 1.3.1...1.4.0

Swift HTTP Types

Swift HTTP Types are version-independent HTTP currency types designed for both clients and servers. They provide a common set of representations for HTTP requests and responses, focusing on modern HTTP features.

Getting Started

Add the following dependency clause to your Package.swift:

dependencies: [
    .package(url: "https://github.com/apple/swift-http-types.git", from: "1.0.0")
]

The HTTPTypes library exposes the core HTTP currency types, including HTTPRequest, HTTPResponse, and HTTPFields.

The HTTPTypesFoundation library provides conveniences for using new HTTP types with Foundation, including bidirectional convertors between the new types and Foundation URL types, and URLSession convenience methods with the new types.

The NIOHTTPTypes, NIOHTTPTypesHTTP1, and NIOHTTPTypesHTTP2 libraries provide channel handlers for translating the version-specific NIO HTTP types with the new HTTP types. They can be found in swift-nio-extras.

Usage

Create a request

let request = HTTPRequest(method: .get, scheme: "https", authority: "www.example.com", path: "/")

Create a request from a Foundation URL

var request = HTTPRequest(method: .get, url: URL(string: "https://www.example.com/")!)
request.method = .post
request.path = "/upload"

Create a response

let response = HTTPResponse(status: .ok)

Access and modify header fields

extension HTTPField.Name {
    static let myCustomHeader = Self("My-Custom-Header")!
}

// Set
request.headerFields[.userAgent] = "MyApp/1.0"
request.headerFields[.myCustomHeader] = "custom-value"
request.headerFields[values: .acceptLanguage] = ["en-US", "zh-Hans-CN"]

// Get
request.headerFields[.userAgent] // "MyApp/1.0"
request.headerFields[.myCustomHeader] // "custom-value"
request.headerFields[.acceptLanguage] // "en-US, zh-Hans-CN"
request.headerFields[values: .acceptLanguage] // ["en-US", "zh-Hans-CN"]

Use with URLSession

var request = HTTPRequest(method: .post, url: URL(string: "https://www.example.com/upload")!)
request.headerFields[.userAgent] = "MyApp/1.0"
let (responseBody, response) = try await URLSession.shared.upload(for: request, from: requestBody)
guard response.status == .created else {
    // Handle error
}

Use with SwiftNIO

channel.configureHTTP2Pipeline(mode: .server) { channel in
    channel.pipeline.addHandlers([
        HTTP2FramePayloadToHTTPServerCodec(),
        ExampleChannelHandler()
    ])
}.map { _ in () }
final class ExampleChannelHandler: ChannelDuplexHandler {
    typealias InboundIn = HTTPTypeServerRequestPart
    typealias OutboundOut = HTTPTypeServerResponsePart

    func channelRead(context: ChannelHandlerContext, data: NIOAny) {
        switch unwrapInboundIn(data) {
        case .head(let request):
            // Handle request headers
        case .body(let body):
            // Handle request body
        case .end(let trailers):
            // Handle complete request
            let response = HTTPResponse(status: .ok)
            context.write(wrapOutboundOut(.head(response)), promise: nil)
            context.writeAndFlush(wrapOutboundOut(.end(nil)), promise: nil)
        }
    }
}

Developing HTTP Types

For the most part, HTTP Types development is as straightforward as any other SwiftPM project. With that said, we do have a few processes that are worth understanding before you contribute. For details, please see CONTRIBUTING.md in this repository.

Please note that all work on HTTP Types is covered by the Swift HTTP Types Code of Conduct.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Thu May 22 2025 04:06:27 GMT-0900 (Hawaii-Aleutian Daylight Time)