Preparing Networking Layer Using Alamofire iOS

The summarized, common, organized way of networking

Let’s implement our first pod Alamofire


Alamofire is an HTTP networking library written in Swift.


CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate Alamofire into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'Alamofire', '~> 5.2'

After implemented the pod run this code in terminal

pod install
  1. Then we create a new file named APIConfiguration.swift

This protocol inherits URLRequestConvertible

Types adopting the `URLRequestConvertible` protocol can be used to safely construct `URLRequest`s.

Basically building the foundation of Requests and manipulate the process by our selves. There are 3 variables in the protocol


1.1 Method

Type of the HTTP Request methods like GET, POST, UPDATE etc.

var method: HTTPMethod { get }

1.2 Path

Presenting our endpoints for different HTTP requests

var path: String { get }

1.3 Parameters

Type of RequestParams we will create after this step.

var parameters: RequestParams { get }

2. Then create our Constants.swift.This file basically stores the static fields we liked to use on our request etc.


baseUrl. → A base URL is, basically, the consistent part of your web address.

HTTPHeaderField → Our requests common headers

ContentType →The Content-Type entity header is used to indicate the media type of the resource.

RequestParams → Our parameters when requesting

3. Then we create a new file APIRouter.swift
Basically does the request with your pre-configured settings.


We basically add our base URL, configure the endpoints and other elements to create our request.

asURLRequest() throws -> URLRequest

And finally, rewrite Alamofire’s asURLRequest() and inside the function, we conform all headers, method, and parameters and return with urlRequest.

That’s easy as you see. It is testable and easy to use and show you how you can use in one line code

To make a login request for our example

First of all, you need a codable response class for handling the response.

Here are the documents for Codable/Decodable Swift

You can always handle response whatever type you want responseString, responseJson, etc. But I used decodable. I recommend to use it because maintained by Apple and you don’t mess with JSON key-value pair.

Response Model

Example Request


Ask anything on comments. Thank you for your time.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store