{"id":15391231,"url":"https://github.com/pjechris/simplehttp","last_synced_at":"2026-04-02T02:59:50.035Z","repository":{"id":39901272,"uuid":"452785672","full_name":"pjechris/SimpleHTTP","owner":"pjechris","description":"Lightweight HTTP framework adding functionalites to URLSession.","archived":false,"fork":false,"pushed_at":"2023-03-20T17:26:00.000Z","size":338,"stargazers_count":7,"open_issues_count":1,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T02:11:57.114Z","etag":null,"topics":["api-client","api-rest","async-await","hacktoberfest","http","http-api","http-client","ios","json","lighweight","network","swift","swift-combine","swift-package-manager","urlrequest","urlsession"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pjechris.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"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},"funding":{"github":["pjechris"]}},"created_at":"2022-01-27T17:49:43.000Z","updated_at":"2025-03-16T03:14:26.000Z","dependencies_parsed_at":"2024-10-18T22:26:52.723Z","dependency_job_id":"933196ef-4f60-4f41-9954-6a9288cfbffd","html_url":"https://github.com/pjechris/SimpleHTTP","commit_stats":{"total_commits":28,"total_committers":4,"mean_commits":7.0,"dds":0.1071428571428571,"last_synced_commit":"cf141359eed7931cfb40dbb77f50e54702b98fe5"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjechris%2FSimpleHTTP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjechris%2FSimpleHTTP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjechris%2FSimpleHTTP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjechris%2FSimpleHTTP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pjechris","download_url":"https://codeload.github.com/pjechris/SimpleHTTP/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249167412,"owners_count":21223503,"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":["api-client","api-rest","async-await","hacktoberfest","http","http-api","http-client","ios","json","lighweight","network","swift","swift-combine","swift-package-manager","urlrequest","urlsession"],"created_at":"2024-10-01T15:10:24.895Z","updated_at":"2026-04-02T02:59:50.025Z","avatar_url":"https://github.com/pjechris.png","language":"Swift","readme":"# SimpleHTTP\n\n![swift](https://img.shields.io/badge/Swift-5.5%2B-orange?logo=swift\u0026logoColor=white)\n![platforms](https://img.shields.io/badge/Platforms-iOS%20%7C%20macOS-lightgrey)\n![tests](https://github.com/pjechris/SimpleHTTP/actions/workflows/test.yml/badge.svg)\n\n[![twitter](https://img.shields.io/badge/twitter-pjechris-1DA1F2?logo=twitter\u0026logoColor=white)](https://twitter.com/pjechris)\n[![doc](https://img.shields.io/badge/read%20the%20doc-8CA1AF?logo=readthedocs\u0026logoColor=white)](https://pjechris.github.io/SimpleHTTP/)\n\nMake HTTP API calls easier. Built on top of URLSession.\n\n## Installation\n\nUse Swift Package Manager to install the library:\n\n```swift\ndependencies: [\n  .package(url: \"https://github.com/pjechris/SimpleHTTP\", from: \"0.4.0\"),\n]\n```\n\nThe package come with 2 modules:\n\n- `SimpleHTTP` which bring the full framework API described in this README\n- `SimpleHTTPFoundation` which only bring a few addition to Foundation API. See this [article](https://swiftunwrap.com/article/designing-http-framework-foundation/) or [API doc](https://pjechris.github.io/SimpleHTTP/) to have a glimpse of what is provided.\n\n## Basic Usage\n\n### Building a request\n\nYou make requests by creating [`Request`](https://pjechris.github.io/SimpleHTTP/Structs/Request.html) objects. You can either create them manually or provide static definition by extending `Request`:\n\n```swift\nextension Request {\n  static let func login(_ body: UserBody) -\u003e Request\u003cUserResponse\u003e {\n    .post(\"login\", body: body)\n  }\n}\n```\n\nThis defines a `Request.login(_:)` method which create a request targeting \"login\" path by sending a `UserBody` and expecting a `UserResponse` as response.\n\n### Sending a request\n\nYou can use your request along `URLSession` by converting it into a `URLRequest` by calling `request.toURLRequest(encoder:relativeTo:accepting)`.\n\nYou can also use a `Session` object. [`Session`](https://pjechris.github.io/SimpleHTTP/Classes/Session.html) is somewhat similar to `URLSession` but providing additional functionalities:\n\n- encoder/decoder for all requests\n- error handling\n- ability to [intercept](#interceptor) requests\n\n```swift\n\nlet session = Session(\n  baseURL: URL(string: \"https://github.com\")!,\n  encoder: JSONEncoder(),\n  decoder: JSONDecoder()\n)\n\ntry await session.response(for: .login(UserBody(username: \"pjechris\", password: \"MyPassword\")))\n\n```\n\nA few words about Session:\n\n- `baseURL` will be prepended to all call paths\n- You can skip encoder and decoder if you use JSON\n- You can provide a custom `URLSession` instance if ever needed\n\n## Send a body\n\nRequest support two body types:\n\n- [Encodable](#encodable)\n- [Multipart](#multipart)\n\n### Encodable\n\nTo send an Encodable object just set it as your Request body:\n\n```swift\nstruct UserBody: Encodable {}\n\nextension Request {\n  static func login(_ body: UserBody) -\u003e Request\u003cLoginResponse\u003e {\n    .post(\"login\", body: body)\n  }\n}\n```\n\n### Multipart\n\nYou can create [multipart](https://pjechris.github.io/SimpleHTTP/Structs/MultipartFormData.html) content from two kind of content\n\n- From a disk file (using a `URL`)\n- From raw content (using `Data`)\n\nFirst example show how to create a request sending an audio file as request body:\n\n```swift\nextension Request {\n  static func send(audioFile: URL) throws -\u003e Request\u003cSendAudioResponse\u003e {\n    var multipart = MultipartFormData()\n\n    try multipart.add(url: audioFile, name: \"define_your_name\")\n\n    return .post(\"v1/sendAudio\", body: multipart)\n  }\n}\n```\n\nSecond example show same request but this time audio file is just some raw unknown data:\n\n```swift\n  static func send(audioFile: Data) throws -\u003e Request\u003cSendAudioResponse\u003e {\n    var multipart = MultipartFormData()\n\n    try multipart.add(data: audioFile, name: \"your_name\", mimeType: \"audioFile_mimeType\")\n\n    return .post(\"v1/sendAudio\", body: multipart)\n  }\n}\n```\n\nNote you can add multiple contents inside a `MultipartFormData`. For instance here we send both a audio file and an image:\n\n```swift\nextension Request {\n  static func send(audio: URL, image: Data) throws -\u003e Request\u003cSendAudioImageResponse\u003e {\n    var multipart = MultipartFormData()\n\n    try multipart.add(url: audio, name: \"define_your_name\")\n    try multipart.add(data: image, name: \"your_name\", mimeType: \"image_mimeType\")\n\n    return .post(\"v1/send\", body: multipart)\n  }\n}\n```\n\n## Constant paths\n\nYou can declare constant paths if needed (refer to Path documentation to see more):\n\n```swift\nextension Path {\n  static let login: Path = \"login\"\n}\n\nextension Request {\n  static let func login(_ body: UserBody) -\u003e Request\u003cUserResponse\u003e {\n    .post(.login, body: body)\n  }\n}\n```\n\n## Interceptor\n\nWhen using Session you can add automatic behavior to your requests/responses using `Interceptor` like authentication, logging, request retrying, etc...\n\n### `RequestInterceptor`\n\n[`RequestInterceptor`](https://pjechris.github.io/SimpleHTTP/Protocols/RequestInterceptor.html) allows to adapt and/or retry a request:\n\n- `adaptRequest` method is called before making a request allowing you to transform it adding headers, changing path, ...\n- `rescueRequestError` is called whenever the request fail. You'll have a chance to retry the request. This can be used to re-authenticate the user for instance\n\n### `ResponseInterceptor`\n\n[`ResponseInterceptor`](https://pjechris.github.io/SimpleHTTP/Protocols/ResponseInterceptor.html) is dedicated to intercept and server responses:\n\n- `adaptResponse` change the server output\n- `receivedResponse` notify about the server final response (a valid output or error)\n","funding_links":["https://github.com/sponsors/pjechris"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpjechris%2Fsimplehttp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpjechris%2Fsimplehttp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpjechris%2Fsimplehttp/lists"}