{"id":25157550,"url":"https://github.com/codecat15/httputility","last_synced_at":"2025-06-16T08:33:14.909Z","repository":{"id":43828655,"uuid":"264782790","full_name":"codecat15/HttpUtility","owner":"codecat15","description":"HttpUtility is an open source MIT license project which is helpful in making HTTP requests and returns a decoded object from server. Right now this utility only parses JSON.","archived":false,"fork":false,"pushed_at":"2023-12-27T03:59:29.000Z","size":132,"stargazers_count":40,"open_issues_count":5,"forks_count":12,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-10T12:56:06.667Z","etag":null,"topics":["api","codecat15","decodable","encodable","httputility","json","parse","request","response","swift","urlrequest","urlsession","xcode"],"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/codecat15.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,"zenodo":null}},"created_at":"2020-05-18T00:20:31.000Z","updated_at":"2025-05-24T02:11:12.000Z","dependencies_parsed_at":"2023-12-27T04:54:11.705Z","dependency_job_id":null,"html_url":"https://github.com/codecat15/HttpUtility","commit_stats":{"total_commits":46,"total_committers":2,"mean_commits":23.0,"dds":0.3913043478260869,"last_synced_commit":"9ac1c9ae7ae49f4fb5ba8d33d521673f8fd26323"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/codecat15/HttpUtility","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecat15%2FHttpUtility","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecat15%2FHttpUtility/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecat15%2FHttpUtility/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecat15%2FHttpUtility/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codecat15","download_url":"https://codeload.github.com/codecat15/HttpUtility/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecat15%2FHttpUtility/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260126584,"owners_count":22962658,"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","codecat15","decodable","encodable","httputility","json","parse","request","response","swift","urlrequest","urlsession","xcode"],"created_at":"2025-02-09T01:24:03.314Z","updated_at":"2025-06-16T08:33:14.882Z","avatar_url":"https://github.com/codecat15.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# HttpUtility\n\nHttpUtility is a light weight open source MIT license project which is helpful in making HTTP requests to the server. It uses URLSession to make requests to the API and returns the [Result enum](https://developer.apple.com/documentation/swift/result) containing the decoded object in case of success or a error incase of failure. Right now this utility only decodes JSON response returned by the server.\n\n[![Build Status](https://travis-ci.com/codecat15/HttpUtility.svg?branch=master)](https://travis-ci.com/codecat15/HttpUtility) [![Twitter](https://img.shields.io/badge/twitter-@codecat15-blue.svg?style=flat)](https://twitter.com/codecat15)\n\n# Purpose of usage\n\nMost of the time iOS application just perform simple HTTP operations which include sending request to the server and getting a response and displaying it to the user. If your iOS app does that then you may use this utility which does not do too much of heavy lifting and just pushes your request to the server and returns you a decoded object.\n\n# Installation\n\n## CocoaPods\n\n[CocoaPods](https://cocoapods.org/) is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate HttpUtility into your Xcode project using CocoaPods, specify it in your Podfile:\n\n```\npod 'HttpUtility', '~\u003e 1.2'\n```\n\n# Using HttpUtility\n\n## Introduction\n\nHttpUtility can be used for basic http operations like get, post, put and delete. It uses [URLSession](https://developer.apple.com/documentation/foundation/urlsession) to perform operations and is just a wrapper around it.\n\nThe best thing about this utility is that it takes a simple URL and returns you a decoded object if the request is successful and returns an error if the request fails. Say good bye to writing loops and custom code to parse JSON response.\n\nGiven are the some of the examples on how you can make use of this utility\n\n1. [Get request](https://github.com/codecat15/HttpUtility#get-request-example)\n2. [Post request](https://github.com/codecat15/HttpUtility#post-request-example)\n3. [Request with query string parameters](https://github.com/codecat15/HttpUtility#get-request-with-query-string-parameters)\n4. [Request with MultiPartFormData](https://github.com/codecat15/HttpUtility#post-request-with-multipartformdata)\n5. [Request with authentication token](https://github.com/codecat15/HttpUtility#authentication-token)\n6. [Customize JSONDecoder in the Utility](https://github.com/codecat15/HttpUtility#token-and-custom-jsondecoder)\n7. [HUNetworkError](https://github.com/codecat15/HttpUtility/tree/multipart-form-data-format#hunetworkerror)\n\n## GET Request example\n\n```swift\nlet utility = HTTPUtility.shared // using the shared instance of the utility to make the API call \nlet requestUrl = URL(string: \"http://demo0333988.mockable.io/Employees\")\nlet request = HURequest(withUrl: requestUrl!, forHttpMethod: .get)\n        \n        utility.request(huRequest: request, resultType: Employees.self) { (response) in\n            switch response\n           {\n            case .success(let employee):\n            // code to handle the response\n\n            case .failure(let error):\n            // your code here to handle error\n\n           }\n        }\n```\n\n## POST request example\n\nThe httpUtility has an extra parameter \"requestBody\" where you should attach the data that you have to post to the server, in the given example the RegisterUserRequest is a struct inheriting from the [Encodable protocol](https://developer.apple.com/documentation/swift/encodable)\n\n```swift\nlet utiltiy = HttpUtility.shared // using the shared instance of the utility to make the API call\n\nlet requestUrl = URL(string: \"https://api-dev-scus-demo.azurewebsites.net/api/User/RegisterUser\")\nlet registerUserRequest = RegisterUserRequest(firstName: \"code\", lastName: \"cat15\", email: \"codecat15@gmail.com\", password: \"1234\")\n\nlet registerUserBody = try! JSONEncoder().encode(registerUserRequest)\nlet request = HURequest(withUrl: requestUrl!, forHttpMethod: .post, requestBody: registerUserBody)\n\n  utility.request(huRequest: request, resultType: RegisterResponse.self) { (response) in\n   switch response\n    {\n      case .success(let registerResponse):\n       // code to handle the response\n\n       case .failure(let error):\n       // your code here to handle error\n\n     }\n```\n\n## GET request with Query string parameters\n\n```swift\nlet utiltiy = HttpUtility.shared // using the shared instance of the utility to make the API call\nlet request = PhoneRequest(color: \"Red\", manufacturer: nil)\n\n// using the extension to convert the encodable request structure to a query string url\nlet requestUrl = request.convertToQueryStringUrl(urlString:\"https://api-dev-scus-demo.azurewebsites.net/api/Product/GetSmartPhone\")\n\nlet request = HURequest(withUrl: requestUrl!, forHttpMethod: .get)\nutility.request(huRequest: request, resultType: PhoneResponse.self) { (response) in\n\n    switch response\n    {\n    case .success(let phoneResponse):\n   // code to handle the response\n\n    case .failure(let error):\n    // your code here to handle error\n\n   }\n}\n```\n\n## POST request with MultiPartFormData\n\n```swift\n\nlet utiltiy = HttpUtility.shared // using the shared instance of the utility to make the API call\nlet requestUrl = URL(string: \"https://api-dev-scus-demo.azurewebsites.net/TestMultiPart\")\n\n// your request model struct should implement the encodable protocol\nlet requestModel = RequestModel(name: \"Bruce\", lastName: \"Wayne\")\n\nlet multiPartRequest = HUMultiPartRequest(url: requestUrl!, method: .post, request: requestModel)\n\nutility.requestWithMultiPartFormData(multiPartRequest: multiPartRequest, responseType: TestMultiPartResponse.self) { (response) in\n            switch response\n            {\n            case .success(let serviceResponse):\n                // code to handle the response\n\n            case .failure(let error):\n                // code to handle failure\n            }\n        }\n```\n\n## Authentication Token\n\n```swift\nlet utility = HttpUtility.shared\nlet token = \"your_token\"\nutility.authenticationToken = token\n```\n\nif you are using a basic or a bearer token then make sure you put basic or bearer before your token starts\n\n### Example: Basic token\n\n```swift\nlet basicToken = \"basic your_token\"\nlet utility = HttpUtility.shared\nutility.authenticationToken = basicToken\n```\n\n### Example: Bearer token\n\n```swift\nlet bearerToken = \"bearer your_token\"\nlet utility = HttpUtility.shared\nutility.authenticationToken = bearerToken\n```\n\n## Custom JSONDecoder\n\nAt times it may happen that you may need to control the behaviour of the default [JSONDecoder](https://developer.apple.com/documentation/foundation/jsondecoder) being used to decode the JSON, for such scenarios the HTTPUtility provides a default init method where you can pass your own custom JSONDecoder and the HTTPUtility will make use of that Decoder and here's how you can do it\n\n```swift\nlet customJsonDecoder = JSONDecoder()\ncustomJsonDecoder.dateEncoding = .millisecondsSince1970\nlet utility = HttpUtility.shared\nutility.customJsonDecoder = customJsonDecoder\n```\n\n## Token and Custom JSONDecoder\n\nAt times when you pass the token and the default JSONDecoder is just not enough, then you may use the init method of the utility to pass the token and a custom JSONDecoder both to make the API request and parse the JSON response\n\n```swift\nlet utility = HttpUtility.shared\nlet customJsonDecoder = JSONDecoder()\ncustomJsonDecoder.dateEncoding = .millisecondsSince1970\n\nlet bearerToken = \"bearer your_token\"\n\nutility.customJsonDecoder = customJsonDecoder\nutility.authenticationToken = bearerToken\n\n```\n\n## HUNetworkError\n\nThe HUNetworkError structure provides in detail description beneficial for debugging purpose, given are the following properties that will be populated in case an error occurs\n\n1. **Status:** This will contain the HTTPStatus code for the request that we receive from the server.\n\n2. **ServerResponse:** This will be the JSON string of the response you received from the server. (not to be confused with error parameter) on error if server returns the error JSON data that message will be decoded to human readable string.\n\n3. **RequestUrl:** The request URL that you just called.\n\n4. **RequestBody:** If you get failure on POST request this property would contain a string representation of the HTTPBody that was sent to the server.\n\n5. **Reason:** This property would contain the debug description from the error closure parameter.\n\nThis utility is for performing basic tasks, and is currently evolving, but if you have any specific feature in mind then please feel free to drop a request and I will try my best to implement it\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodecat15%2Fhttputility","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodecat15%2Fhttputility","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodecat15%2Fhttputility/lists"}