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.
func getData() {
// Create a URLSession shared instance
let session = URLSession.shared
shared property provides a default session configuration that is suitable for most tasks.// Define the URL to fetch data from
let serviceUrl = URL(string: "<https://jsonplaceholder.typicode.com/todos/1>")
URL object from a string representing the endpoint you want to fetch data from.// Create a data task to fetch data from the URL
let task = session.dataTask(with: serviceUrl!) { (serviceData, serviceResponse, error) in
URLSessionDataTask to perform an HTTP request to the specified URL. The closure provided is called when the task completes, handling the data, response, and error.// Check if there was no error
if error == nil {
elseblock.// Safely cast the response to HTTPURLResponse
let httpResponse = serviceResponse as! HTTPURLResponse
URLResponse to HTTPURLResponse to access HTTP-specific properties like the status code.// Check if the status code indicates success (200 OK)
if httpResponse.statusCode == 200 {
else block handles it.