{"id":13496791,"url":"https://github.com/apple/swift-http-types","last_synced_at":"2025-05-14T15:11:36.117Z","repository":{"id":179003760,"uuid":"567482582","full_name":"apple/swift-http-types","owner":"apple","description":"Version-independent HTTP currency types for Swift","archived":false,"fork":false,"pushed_at":"2025-04-04T20:12:35.000Z","size":172,"stargazers_count":942,"open_issues_count":20,"forks_count":56,"subscribers_count":33,"default_branch":"main","last_synced_at":"2025-04-05T19:06:31.502Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/apple.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-17T22:14:21.000Z","updated_at":"2025-04-04T02:12:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"6cb219e9-dd08-4a12-9537-b6bf182e9c00","html_url":"https://github.com/apple/swift-http-types","commit_stats":{"total_commits":69,"total_committers":13,"mean_commits":"5.3076923076923075","dds":0.4347826086956522,"last_synced_commit":"baf704f113c8b34ac3ccba1a12b0d7be63f6caaa"},"previous_names":["apple/swift-http-types"],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apple%2Fswift-http-types","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apple%2Fswift-http-types/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apple%2Fswift-http-types/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apple%2Fswift-http-types/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apple","download_url":"https://codeload.github.com/apple/swift-http-types/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248631612,"owners_count":21136553,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T19:01:59.876Z","updated_at":"2025-04-12T20:37:52.986Z","avatar_url":"https://github.com/apple.png","language":"Swift","funding_links":[],"categories":["Swift"],"sub_categories":[],"readme":"# Swift HTTP Types\n\nSwift 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.\n\n## Getting Started\n\nAdd the following dependency clause to your Package.swift:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/apple/swift-http-types.git\", from: \"1.0.0\")\n]\n```\n\nThe `HTTPTypes` library exposes the core HTTP currency types, including `HTTPRequest`, `HTTPResponse`, and `HTTPFields`.\n\nThe `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.\n\nThe `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`](https://github.com/apple/swift-nio-extras).\n\n## Usage\n\n#### Create a request\n\n```swift\nlet request = HTTPRequest(method: .get, scheme: \"https\", authority: \"www.example.com\", path: \"/\")\n```\n\n#### Create a request from a Foundation URL\n\n```swift\nvar request = HTTPRequest(method: .get, url: URL(string: \"https://www.example.com/\")!)\nrequest.method = .post\nrequest.path = \"/upload\"\n```\n\n#### Create a response\n\n```swift\nlet response = HTTPResponse(status: .ok)\n```\n\n#### Access and modify header fields\n\n```swift\nextension HTTPField.Name {\n    static let myCustomHeader = Self(\"My-Custom-Header\")!\n}\n\n// Set\nrequest.headerFields[.userAgent] = \"MyApp/1.0\"\nrequest.headerFields[.myCustomHeader] = \"custom-value\"\nrequest.headerFields[values: .acceptLanguage] = [\"en-US\", \"zh-Hans-CN\"]\n\n// Get\nrequest.headerFields[.userAgent] // \"MyApp/1.0\"\nrequest.headerFields[.myCustomHeader] // \"custom-value\"\nrequest.headerFields[.acceptLanguage] // \"en-US, zh-Hans-CN\"\nrequest.headerFields[values: .acceptLanguage] // [\"en-US\", \"zh-Hans-CN\"]\n```\n\n#### Use with URLSession\n\n```swift\nvar request = HTTPRequest(method: .post, url: URL(string: \"https://www.example.com/upload\")!)\nrequest.headerFields[.userAgent] = \"MyApp/1.0\"\nlet (responseBody, response) = try await URLSession.shared.upload(for: request, from: requestBody)\nguard response.status == .created else {\n    // Handle error\n}\n```\n\n#### Use with SwiftNIO\n\n```swift\nchannel.configureHTTP2Pipeline(mode: .server) { channel in\n    channel.pipeline.addHandlers([\n        HTTP2FramePayloadToHTTPServerCodec(),\n        ExampleChannelHandler()\n    ])\n}.map { _ in () }\n```\n\n```swift\nfinal class ExampleChannelHandler: ChannelDuplexHandler {\n    typealias InboundIn = HTTPTypeServerRequestPart\n    typealias OutboundOut = HTTPTypeServerResponsePart\n\n    func channelRead(context: ChannelHandlerContext, data: NIOAny) {\n        switch unwrapInboundIn(data) {\n        case .head(let request):\n            // Handle request headers\n        case .body(let body):\n            // Handle request body\n        case .end(let trailers):\n            // Handle complete request\n            let response = HTTPResponse(status: .ok)\n            context.write(wrapOutboundOut(.head(response)), promise: nil)\n            context.writeAndFlush(wrapOutboundOut(.end(nil)), promise: nil)\n        }\n    }\n}\n```\n\n## Developing HTTP Types\n\nFor 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.\n\nPlease note that all work on HTTP Types is covered by the [Swift HTTP Types Code of Conduct](https://github.com/apple/swift-http-types/blob/main/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapple%2Fswift-http-types","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapple%2Fswift-http-types","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapple%2Fswift-http-types/lists"}