Swift Playground REST Request

Updated: 2021-05-08

You can run this example in Swift Playground using MacOS.

Replace _YOUR_ENDPOINT with the url of your REST Endpoint.

// Foundation is required for URLSession 
import Foundation 
// PlaygroundSupport: https://developer.apple.com/documentation/playgroundsupport 
import PlaygroundSupport 
let url = URL(string: "[YOUR_ENDPOINT]")! 
// needsIndefiniteExecution is required for async code https://developer.apple.com/documentation/playgroundsupport/playgroundpage/1964501-needsindefiniteexecution 
PlaygroundPage.current.needsIndefiniteExecution = true 
// shared.dataTask saves the data in memory 
let task = URLSession.shared.dataTask(with: url) { 
    // @escaping closure 
    // https://docs.swift.org/swift-book/LanguageGuide/Closures.html 
    (data, response, error) in 
    // guard is like if but exit if the check fails 
    guard error == nil else { 
    guard let responseData = data else { 
        print("Error: no data") 
    guard let httpResponse = response as? HTTPURLResponse else { 
        print("Error: Response Error") 
    // we decode the json answer and we print it 
    let content = String(decoding: data!, as: UTF8.self) 
    print("Response data: \(content)") 
// start the task 

Fullstack Angular / Java application quick start guide.
WebApp built by Marco using SpringBoot 3.2.4 and Java 21. Hosted in Switzerland (GE8).