The HTTP GET method is used to request data from a specified resource on a server. When a client (such as a web browser or mobile app) performs a GET request, it is asking the server to return the resource or data associated with the URL provided. GET requests are typically used to retrieve data without making any changes to the resource. For example, when you visit a webpage or fetch user details from a server, you're often using a GET request. The server processes the request and sends back the requested data, which could be in various formats such as HTML, JSON, or XML. GET requests are usually idempotent, meaning that multiple identical GET requests should result in the same server response and should not cause any side effects.

Function Definition

func getData() {

URLSession and Data Task Creation

// Create a URLSession shared instance
let session = URLSession.shared
// Define the URL to fetch data from
let serviceUrl = URL(string: "<https://jsonplaceholder.typicode.com/todos/1>")
// Create a data task to fetch data from the URL
let task = session.dataTask(with: serviceUrl!) { (serviceData, serviceResponse, error) in

Handling the Response

// Check if there was no error
if error == nil {
// Safely cast the response to HTTPURLResponse
let httpResponse = serviceResponse as! HTTPURLResponse
// Check if the status code indicates success (200 OK)
if httpResponse.statusCode == 200 {

JSON Parsing