Skip to content

Instantly share code, notes, and snippets.

View cmoulton's full-sized avatar

Christina Moulton cmoulton

View GitHub Profile
@cmoulton
cmoulton / URLSession Calls in Swift 3.0.1
Last active November 4, 2022 00:29
URLSession Calls in Swift 3.0.1
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
@cmoulton
cmoulton / Custom HTTP Headers with Swift and Alamofire.swift
Last active July 15, 2022 12:37
Custom HTTP Headers with Swift 3 or 4 and Alamofire 4.0-4.7: See https://grokswift.com/custom-headers-alamofire4-swift3/ for explanations
// MARK: - Adding a header to a single request
func doRequestWithHeaders1() {
let headers: HTTPHeaders = [
"X-Mashape-Key": MY_API_KEY,
"Accept": "application/json"
]
Alamofire.request("https://mashape-community-urban-dictionary.p.mashape.com/define?term=smh", headers: headers)
.responseJSON { response in
debugPrint(response)
@cmoulton
cmoulton / Simple Alamofire Calls in Swift 3.0.1
Last active January 25, 2024 13:56
Simple Alamofire Calls in Swift 3.0.1
func alamofireGet() {
let todoEndpoint: String = "https://jsonplaceholder.typicode.com/todos/1"
Alamofire.request(todoEndpoint)
.responseJSON { response in
// check for errors
guard response.result.error == nil else {
// got an error in getting the data, need to handle it
print("error calling GET on /todos/1")
print(response.result.error!)
return
@cmoulton
cmoulton / Todo+Networking.swift
Created November 16, 2016 16:02
Demo code for Strongly-Typed GET and POST Calls With Alamofire at https://grokswift.com/strongly-typed-api-calls/. Uses Swift 3.0 and Alamofire 4.0.
import Foundation
import Alamofire
enum BackendError: Error {
case objectSerialization(reason: String)
}
extension Todo {
class func endpointForID(_ id: Int) -> String {
return "https://jsonplaceholder.typicode.com/todos/\(id)"
@cmoulton
cmoulton / URLSession Calls in Swift 4
Last active December 18, 2023 02:31
URLSession Calls in Swift 4
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
@cmoulton
cmoulton / Simple Alamofire Calls in Swift 4
Last active December 8, 2020 09:29
Simple Alamofire Calls in Swift 4
import Alamofire
func makeGetCallWithAlamofire() {
let todoEndpoint: String = "https://jsonplaceholder.typicode.com/todos/1"
Alamofire.request(todoEndpoint)
.responseJSON { response in
// check for errors
guard response.result.error == nil else {
// got an error in getting the data, need to handle it
print("error calling GET on /todos/1")