-
-
Save cmoulton/7ddc3cfabda1facb040a533f637e74b8 to your computer and use it in GitHub Desktop.
| func makeGetCall() { | |
| // Set up the URL request | |
| let todoEndpoint: String = "https://jsonplaceholder.typicode.com/todos/1" | |
| guard let url = URL(string: todoEndpoint) else { | |
| print("Error: cannot create URL") | |
| return | |
| } | |
| let urlRequest = URLRequest(url: url) | |
| // set up the session | |
| let config = URLSessionConfiguration.default | |
| let session = URLSession(configuration: config) | |
| // make the request | |
| let task = session.dataTask(with: urlRequest) { | |
| (data, response, error) in | |
| // check for any errors | |
| guard error == nil else { | |
| print("error calling GET on /todos/1") | |
| print(error!) | |
| return | |
| } | |
| // make sure we got data | |
| guard let responseData = data else { | |
| print("Error: did not receive data") | |
| return | |
| } | |
| // parse the result as JSON, since that's what the API provides | |
| do { | |
| guard let todo = try JSONSerialization.jsonObject(with: responseData, options: []) | |
| as? [String: Any] else { | |
| print("error trying to convert data to JSON") | |
| return | |
| } | |
| // now we have the todo | |
| // let's just print it to prove we can access it | |
| print("The todo is: " + todo.description) | |
| // the todo object is a dictionary | |
| // so we just access the title using the "title" key | |
| // so check for a title and print it if we have one | |
| guard let todoTitle = todo["title"] as? String else { | |
| print("Could not get todo title from JSON") | |
| return | |
| } | |
| print("The title is: " + todoTitle) | |
| } catch { | |
| print("error trying to convert data to JSON") | |
| return | |
| } | |
| } | |
| task.resume() | |
| } | |
| func makePostCall() { | |
| let todosEndpoint: String = "https://jsonplaceholder.typicode.com/todos" | |
| guard let todosURL = URL(string: todosEndpoint) else { | |
| print("Error: cannot create URL") | |
| return | |
| } | |
| var todosUrlRequest = URLRequest(url: todosURL) | |
| todosUrlRequest.httpMethod = "POST" | |
| let newTodo: [String: Any] = ["title": "My First todo", "completed": false, "userId": 1] | |
| let jsonTodo: Data | |
| do { | |
| jsonTodo = try JSONSerialization.data(withJSONObject: newTodo, options: []) | |
| todosUrlRequest.httpBody = jsonTodo | |
| } catch { | |
| print("Error: cannot create JSON from todo") | |
| return | |
| } | |
| let session = URLSession.shared | |
| let task = session.dataTask(with: todosUrlRequest) { | |
| (data, response, error) in | |
| guard error == nil else { | |
| print("error calling POST on /todos/1") | |
| print(error!) | |
| return | |
| } | |
| guard let responseData = data else { | |
| print("Error: did not receive data") | |
| return | |
| } | |
| // parse the result as JSON, since that's what the API provides | |
| do { | |
| guard let receivedTodo = try JSONSerialization.jsonObject(with: responseData, | |
| options: []) as? [String: Any] else { | |
| print("Could not get JSON from responseData as dictionary") | |
| return | |
| } | |
| print("The todo is: " + receivedTodo.description) | |
| guard let todoID = receivedTodo["id"] as? Int else { | |
| print("Could not get todoID as int from JSON") | |
| return | |
| } | |
| print("The ID is: \(todoID)") | |
| } catch { | |
| print("error parsing response from POST on /todos") | |
| return | |
| } | |
| } | |
| task.resume() | |
| } | |
| func makeDeleteCall() { | |
| let firstTodoEndpoint: String = "https://jsonplaceholder.typicode.com/todos/1" | |
| var firstTodoUrlRequest = URLRequest(url: URL(string: firstTodoEndpoint)!) | |
| firstTodoUrlRequest.httpMethod = "DELETE" | |
| let session = URLSession.shared | |
| let task = session.dataTask(with: firstTodoUrlRequest) { | |
| (data, response, error) in | |
| guard let _ = data else { | |
| print("error calling DELETE on /todos/1") | |
| return | |
| } | |
| print("DELETE ok") | |
| } | |
| task.resume() | |
| } |
Hi ShanghaiTimes,
As URLSessionDataTask is an asynchronous method you will have to pass a closure (aka completionHandler) as a parameter in makeGetCall() method. Once the response is obtained you can pass the value in this closure.
eg:
func makeGetCall(completionHandler: (Int) -> ()) {
/// All previous code
//
//
print("The todo is: " + receivedTodo.description)
guard let todoID = receivedTodo["id"] as? Int else {
print("Could not get todoID as int from JSON")
return
}
completionHandler(todoID)
} catch {
/// Remaining code
///
///
}
and this is how you make a call to the makeGetCall function:
makeGetCall() { todoTitle in
print("The title is: " + todoTitle)
}
I found this gist useful! When testing it against a toy API I built (instead of against jsonplaceholder) I found that I needed to set the content-type header explicitly on my POST requests. I added this line after todosUrlRequest.httpMethod = "POST":
todosUrlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
Sharing my discovery here in case it helps someone else in their travels
Hi ,
I try to get HTML code from a web page behind a user login password page.
The users and password is sent into the URL at the first connexion. The web Site give me 3 cookie.
when a call the func with completionHandler(htmlinfo) i got an error from my website who tell me that i'm not authentificate (problem of cookiee )
do you know how i could keep cookie from the func ?
func makeGetCall() {
// Set up the URL request
let todoEndpoint: String = "https://jsonplaceholder.typicode.com/todos/1"
guard let url = URL(string: todoEndpoint) else {
print("Error: cannot create URL")
return
}
let urlRequest = URLRequest(url: url)
// set up the session
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
// make the request
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET on /todos/1")
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
guard let todo = try JSONSerialization.jsonObject(with: responseData, options: [])
as? [String: Any] else {
print("error trying to convert data to JSON")
return
}
// now we have the todo
// let's just print it to prove we can access it
print("The todo is: " + todo.description)
// the todo object is a dictionary
// so we just access the title using the "title" key
// so check for a title and print it if we have one
guard let todoTitle = todo["title"] as? String else {
print("Could not get todo title from JSON")
return
}
print("The title is: " + todoTitle)
} catch {
print("error trying to convert data to JSON")
return
}
}
task.resume()
}
func makePostCall() {
let todosEndpoint: String = "https://jsonplaceholder.typicode.com/todos"
guard let todosURL = URL(string: todosEndpoint) else {
print("Error: cannot create URL")
return
}
var todosUrlRequest = URLRequest(url: todosURL)
todosUrlRequest.httpMethod = "POST"
let newTodo: [String: Any] = ["title": "My First todo", "completed": false, "userId": 1]
let jsonTodo: Data
do {
jsonTodo = try JSONSerialization.data(withJSONObject: newTodo, options: [])
todosUrlRequest.httpBody = jsonTodo
} catch {
print("Error: cannot create JSON from todo")
return
}
let session = URLSession.shared
let task = session.dataTask(with: todosUrlRequest) {
(data, response, error) in
guard error == nil else {
print("error calling POST on /todos/1")
print(error!)
return
}
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
guard let receivedTodo = try JSONSerialization.jsonObject(with: responseData,
options: []) as? [String: Any] else {
print("Could not get JSON from responseData as dictionary")
return
}
print("The todo is: " + receivedTodo.description)
guard let todoID = receivedTodo["id"] as? Int else {
print("Could not get todoID as int from JSON")
return
}
print("The ID is: \(todoID)")
} catch {
print("error parsing response from POST on /todos")
return
}
}
task.resume()
}
func makeDeleteCall() {
let firstTodoEndpoint: String = "https://jsonplaceholder.typicode.com/todos/1"
var firstTodoUrlRequest = URLRequest(url: URL(string: firstTodoEndpoint)!)
firstTodoUrlRequest.httpMethod = "DELETE"
let session = URLSession.shared
let task = session.dataTask(with: firstTodoUrlRequest) {
(data, response, error) in
guard let _ = data else {
print("error calling DELETE on /todos/1")
return
}
print("DELETE ok")
}
task.resume()
}Applied swift syntax highlight.
I have a question. How do I return a value from this
func makeGetCall() {
// Set up the URL request
so I can put it into a storyboard label and print it out?