make a get request with alamofire in swift

Here's an example of how to make a GET request using Alamofire in Swift:

main.swift
import Alamofire

AF.request("https://jsonplaceholder.typicode.com/posts")
    .responseJSON { response in
        
        if let error = response.error {
            print("Error fetching data: \(error)")
            return
        }
        
        if let data = response.data {
            // Handle JSON response data
            print(data)
        }
    }
364 chars
16 lines

This code sends a GET request to the JSONPlaceholder API and logs the response data to the console. The responseJSON method tells Alamofire to parse the response data as JSON and returns a DataResponse<Any> object. You can then check if there are any errors or extract the response data from the data property of the response object.

gistlibby LogSnag