get request from html swift
//To this, we have two main ways of getting information from the web:
//URLSession and NSURLSession
//It is highly reconmened to use URLSession (easier and newer)
//URLSession way:
//(Make sure you have imported Foundation)
let url = URL(string: String)
//This converts the string parameter into a url.
let urlRequest = URLRequest(url: URL)
//This sorts of makes the request
let task = URLSession.shared.dataTask(with: urlRequest) { data, response, error in
//The URLSessions.shared part gets data from the url using the urlRequest, albeit with some limitations
//https://developer.apple.com/documentation/foundation/urlsession/1409000-shared
/*
data -> data received from the url (if applicable)
This is more applicable if getting information from an api by json or other forms
response -> response from the web server
Is of type URLResponse, and can tell us a lot such as the http request code (etc 404) and others
error -> errors that might pop up
Is of type error
*/
//For now, we will be focusing on simply getting the data
if let data = data {
print(String(data: data encoding: .utf8))
//Here, we are using utf-8 to decode the data received into a string, so that we can print it out
//Choose the right kind of encoding, as different encodings can have completely different results
}
}
task.resume()
//The resume() is so that the request can actually send