make http get request in swift

To make an HTTP GET request in Swift, you can use the URLSession class provided by the iOS SDK. Here's an example code snippet:

main.swift
if let url = URL(string: "https://jsonplaceholder.typicode.com/posts/1") {
   let session = URLSession.shared
   session.dataTask(with: url) { (data, response, error) in
      if let response = response {
         print(response)
      }
      if let data = data {
         print(String(data: data, encoding: .utf8)!)
      }
   }.resume()
}
342 chars
12 lines

This code creates a URL object for the endpoint you want to query, then creates a URLSession object and uses its dataTask method to make a GET request to that URL. When the request completes, the code prints the response and response data (if available).

Note that in a real-world scenario, you would want to handle errors and parse the response data in a more robust way than what's shown here.

gistlibby LogSnag