{"id":26319138,"url":"https://github.com/ralfebert/sweeturlrequest","last_synced_at":"2025-03-15T15:17:54.534Z","repository":{"id":66513609,"uuid":"262793193","full_name":"ralfebert/SweetURLRequest","owner":"ralfebert","description":"SweetURLRequest simplifies common cases for creating an URLRequest and handling HTTPURLResponse.","archived":false,"fork":false,"pushed_at":"2024-12-17T17:20:10.000Z","size":44,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-17T18:27:54.433Z","etag":null,"topics":["http","ios","swift","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/ralfebert.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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}},"created_at":"2020-05-10T13:28:49.000Z","updated_at":"2024-12-17T17:20:00.000Z","dependencies_parsed_at":"2023-03-07T16:45:32.045Z","dependency_job_id":null,"html_url":"https://github.com/ralfebert/SweetURLRequest","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralfebert%2FSweetURLRequest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralfebert%2FSweetURLRequest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralfebert%2FSweetURLRequest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralfebert%2FSweetURLRequest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ralfebert","download_url":"https://codeload.github.com/ralfebert/SweetURLRequest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243746238,"owners_count":20341204,"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":["http","ios","swift","urlsession"],"created_at":"2025-03-15T15:17:53.547Z","updated_at":"2025-03-15T15:17:54.526Z","avatar_url":"https://github.com/ralfebert.png","language":"Swift","readme":"# SweetURLRequest\n\nSweetURLRequest simplifies common cases for creating an URLRequest and handling HTTPURLResponse.\n\nExtensions for URLRequest:\n\n⚡ Constants for HTTP methods  \n⚡ Properties to set common HTTP headers  \n⚡ URL/Form/JSON encoded parameters  \n\nExtensions for HTTPURLResponse:\n\n⚡ Check for a 2xx success status code  \n⚡ Handle HTTP status codes via switch/case  \n⚡ Error enum for HTTP status codes\n\n## Examples\n\n### Example project\n\n [MetMuseumEndpoints: A Swift package for the The Metropolitan Museum of Art Collection API](https://github.com/ralfebert/MetMuseumEndpoints)\n\n### Constants for HTTP methods\n\n```swift\nURLRequest(\n    method: .post,\n    url: URL(string: \"http://www.example.com\")!\n)\n```\n\n### Properties to set common HTTP headers\n\n```swift\nvar request = URLRequest(url: URL(string: \"http://www.example.com\")!)\nrequest.headers.accept = .json\nrequest.headers.contentType = .xml\nrequest.headers.authorization = \"Bearer xyz\"\n```\n\n### URL/Form/JSON encoded parameters\n\nParameters will be URL-encoded for GET/HEAD/DELETE and sent as application/x-www-form-urlencoded body for other methods by default:\n\n```swift\nURLRequest(\n    method: .get,\n    url: URL(string: \"http://www.example.com\")!,\n    parameters: [\"param1\" : \"äöü\", \"param2\" : \"foo bar\"]\n)\n```\n\nYou can also pass a JSON body, this will use JSONEncoder to serialize the given data and set an appropriate Content-Type header:\n\n```swift\ntry URLRequest(\n    method: .post,\n    url: URL(string: \"http://www.example.com\")!,\n    jsonBody: Person(name: \"Bob\")\n)\n```\n\n### Check for a success status code in the 2xx range\n\n```swift\nfunc expectSuccess(response: URLResponse) throws {\n    let status = (response as! HTTPURLResponse).status\n    guard status.responseType == .success else { throw status }\n}\n```\n\n### Handle HTTP status codes via switch/case, throw a status code as Error\n\n```swift\nfunc validate(response: URLResponse) throws {\n    let status = (response as! HTTPURLResponse).status\n\n    guard status.responseType == .success else {\n        switch status {\n        case .unauthorized:\n            print(\"Unauthorized\")\n        default:\n            print(\"Non-success status: \\(status)\")\n        }\n        throw status\n    }\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fralfebert%2Fsweeturlrequest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fralfebert%2Fsweeturlrequest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fralfebert%2Fsweeturlrequest/lists"}