Fetching data through Get request using Alamofire

In the previous section of the tutorial, we have installed Alamofire and set it up to make request to the application programming interface (API), which transmits data between the server and iOS application. In this section, we will see how we can fetch the data through Get request using Alamofire.

Get Request

Making a get request using alamofire is very simple. The request method of Alamofire is used for this purpose. The syntax is given below.

While making get request using alamofire, we only need to have the API URL. On success, we get a response object using which we can get the response result value, which can be parsed in our response model.

Example

In this example, we will use an Apple dummy API URL https://itunes.apple.com/search?media=music&term=bollywood. Here, we will create an application, which contains a dynamic table view to display API data to the user. In this application, we will make a get request to the API and then populate the API data to the tableview.

Follow the steps given in the previous chapter to install Alamofire in the application and open the xcworkspace file. Create a MainViewController class and make a get request to print the API data using Alamofire.

MainViewController.swift

The above application will print the response in the console, as shown below.

Fetching data through Get request using Alamofire

Here, the result count is coming to be 50. Here, we will create our interface builder using tableview to present this data.

Main.storyboard

In the storyboard file, we will add a tableview inside the UIViewController. Inside the Prototype cell, we will add an imageview and the labels to show the Collection Artist name, artist country, and artist name, respectively. We will assign the MainTableVIewCell subclass to this cell and create the respective outlets.

Fetching data through Get request using Alamofire

MainTableViewCell.swift

ViewController.swift

Output

Fetching data through Get request using Alamofire






Latest Courses