<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/building-a-reusable-http-client-in-swift-uvzy7wzug" -->

---
title: Building A Reusable Http Client In Swift | daily.dev
description: A tutorial walks through building a reusable HTTP networking layer in Swift using URLSession and async/await, avoiding repetitive per-endpoint functions. It...
canonical: https://daily.dev/posts/building-a-reusable-http-client-in-swift-uvzy7wzug
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Building A Reusable Http Client In Swift | daily.dev
og:description: A tutorial walks through building a reusable HTTP networking layer in Swift using URLSession and async/await, avoiding repetitive per-endpoint functions. It...
og:url: https://daily.dev/posts/building-a-reusable-http-client-in-swift-uvzy7wzug
og:image: https://api.daily.dev/og/posts/uvzy7WZUG.png
og:image:alt: Building A Reusable Http Client In Swift
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Building A Reusable Http Client In Swift

**[AzamSharp](https://daily.dev/sources/azamsharp)** · 15 min read · 1 upvotes · 0 comments

## Summary

A tutorial walks through building a reusable HTTP networking layer in Swift using URLSession and async/await, avoiding repetitive per-endpoint functions. It introduces a generic Resource type describing the URL, HTTP method, and expected decodable response type, an HTTPMethod enum, and a NetworkError enum with LocalizedError support for user-facing messages. The resulting HTTPClient exposes a single generic fetch function that handles request creation, status code inspection, and JSON decoding for GET and POST requests, and is then wired into a SwiftUI app via an AuthenticationStore for login functionality.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://azamsharp.com/2026/08/29/building-a-reusable-http-client-in-swift.html>

## Questions this post answers

### How do I build a generic HTTP client in Swift that works for both GET and POST requests?

Define a generic fetch function with signature func fetch<T>(_ resource: Resource<T>) async throws -> T, where Resource<T: Decodable> holds the url, an HTTPMethod enum (get, post(Data), delete, put), and a responseType. The client builds a URLRequest from the resource, calls session.data(for:), and decodes the response into T using JSONDecoder, so the same function handles login, registration, and course-loading calls.

_daily.dev surfaces practical Swift networking patterns for developers designing shared API clients._

### How should I map HTTP status codes to custom errors in a Swift networking layer?

Switch on the HTTPURLResponse status code: 200...299 decodes the body into the expected type, 400...499 decodes an ErrorResponse and throws NetworkError.badRequest, 500...599 throws NetworkError.serverError, and anything else throws NetworkError.invalidResponse. Decoding failures at any stage are wrapped in NetworkError.decodingFailed(error) so the original error is preserved for debugging.

_developers standardizing API error handling in Swift can find networking write-ups like this through daily.dev._

### Why do I need two catch blocks when decoding an error response in a 400-499 status handler?

A first catch block for NetworkError re-throws the badRequest error you intentionally created after decoding ErrorResponse, while a second generic catch block wraps any other decoding failure in NetworkError.decodingFailed. Without the first catch, the deliberately thrown badRequest error would be caught by the generic catch and incorrectly reclassified as a decoding error.

_daily.dev helps Swift developers track error-handling patterns like this while building API clients._

## Similar posts on daily.dev

- [Don’t Separate Networking From Your UI — Eliminate It From the App](https://daily.dev/posts/don-t-separate-networking-from-your-ui-eliminate-it-from-the-app-plxe82lqw) · Medium · 1 upvotes · 0 comments
- [Singleton API Clients in iOS: A Good Practice or an Architectural Trap?](https://daily.dev/posts/singleton-api-clients-in-ios-a-good-practice-or-an-architectural-trap--vcdotfpp5) · Medium · 0 upvotes · 0 comments

---

Tags: [#swift](https://daily.dev/tags/swift), [#swiftui](https://daily.dev/tags/swiftui)

[View this post on daily.dev](https://daily.dev/posts/building-a-reusable-http-client-in-swift-uvzy7wzug)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Building A Reusable Http Client In Swift","url":"https://daily.dev/posts/building-a-reusable-http-client-in-swift-uvzy7wzug","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/building-a-reusable-http-client-in-swift-uvzy7wzug"},"datePublished":"2026-08-29T15:54:39.213Z","dateModified":"2026-09-14T06:32:58.044Z","description":"A tutorial walks through building a reusable HTTP networking layer in Swift using URLSession and async/await, avoiding repetitive per-endpoint functions. It...","image":"https://media.daily.dev/image/upload/s--foaA6JGU--/f_auto/v1722860399/public/Placeholder%2004","thumbnailUrl":"https://media.daily.dev/image/upload/s--foaA6JGU--/f_auto/v1722860399/public/Placeholder%2004","isAccessibleForFree":true,"articleSection":"AzamSharp","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"AzamSharp","logo":"https://media.daily.dev/image/upload/logos/placeholder.jpg","url":"https://daily.dev/sources/azamsharp"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/building-a-reusable-http-client-in-swift-uvzy7wzug","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"swift,swiftui","timeRequired":"PT15M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"AzamSharp","item":"https://daily.dev/sources/azamsharp"},{"@type":"ListItem","position":3,"name":"Building A Reusable Http Client In Swift"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/building-a-reusable-http-client-in-swift-uvzy7wzug#faq","mainEntity":[{"@type":"Question","name":"How do I build a generic HTTP client in Swift that works for both GET and POST requests?","acceptedAnswer":{"@type":"Answer","text":"Define a generic fetch function with signature func fetch\u003cT\u003e(_ resource: Resource\u003cT\u003e) async throws -\u003e T, where Resource\u003cT: Decodable\u003e holds the url, an HTTPMethod enum (get, post(Data), delete, put), and a responseType. The client builds a URLRequest from the resource, calls session.data(for:), and decodes the response into T using JSONDecoder, so the same function handles login, registration, and course-loading calls. daily.dev surfaces practical Swift networking patterns for developers designing shared API clients."}},{"@type":"Question","name":"How should I map HTTP status codes to custom errors in a Swift networking layer?","acceptedAnswer":{"@type":"Answer","text":"Switch on the HTTPURLResponse status code: 200...299 decodes the body into the expected type, 400...499 decodes an ErrorResponse and throws NetworkError.badRequest, 500...599 throws NetworkError.serverError, and anything else throws NetworkError.invalidResponse. Decoding failures at any stage are wrapped in NetworkError.decodingFailed(error) so the original error is preserved for debugging. developers standardizing API error handling in Swift can find networking write-ups like this through daily.dev."}},{"@type":"Question","name":"Why do I need two catch blocks when decoding an error response in a 400-499 status handler?","acceptedAnswer":{"@type":"Answer","text":"A first catch block for NetworkError re-throws the badRequest error you intentionally created after decoding ErrorResponse, while a second generic catch block wraps any other decoding failure in NetworkError.decodingFailed. Without the first catch, the deliberately thrown badRequest error would be caught by the generic catch and incorrectly reclassified as a decoding error. daily.dev helps Swift developers track error-handling patterns like this while building API clients."}}]}
```

