Forked from cmoulton/Custom HTTP Headers with Swift and Alamofire.swift
Created
December 9, 2018 19:59
-
-
Save gerson/142104ac162f9d517c5126f67881da9b to your computer and use it in GitHub Desktop.
Custom HTTP Headers with Swift 3 or 4 and Alamofire 4.0-4.7: See https://grokswift.com/custom-headers-alamofire4-swift3/ for explanations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} | |
// MARK: - To make sure the headers are sent, use debugPrint on the request | |
let request = Alamofire.request("https://mashape-community-urban-dictionary.p.mashape.com/define?term=smh", headers: headers) | |
.responseJSON { response in | |
debugPrint(response) | |
} | |
debugPrint(request) | |
} | |
// MARK: - Adding headers to all requests in the session | |
// store sessionManager so it doesn't get deallocated while we're waiting for the network call to finish | |
var sessionManager: Alamofire.SessionManager? | |
func doRequestWithHeaders2() { | |
// get the default headers | |
var headers = Alamofire.SessionManager.defaultHTTPHeaders | |
// add your custom headers | |
headers["API-Version"] = "2.0" | |
headers["X-Mashape-Key"] = MY_API_KEY | |
// create a custom session configuration | |
let configuration = URLSessionConfiguration.default | |
// add the headers | |
configuration.httpAdditionalHeaders = headers | |
// create a session manager with the configuration | |
self.sessionManager = Alamofire.SessionManager(configuration: configuration) | |
// make calls with the session manager | |
sessionManager?.request("https://mashape-community-urban-dictionary.p.mashape.com/define?term=smh") | |
.responseJSON { response in | |
debugPrint(response) | |
} | |
} | |
// MARK: - Add headers when using a URLRequest | |
func doRequestWithHeaders3() { | |
if let url = URL(string: "https://mashape-community-urban-dictionary.p.mashape.com/define?term=smh") { | |
var urlRequest = URLRequest(url: url) | |
urlRequest.httpMethod = HTTPMethod.get.rawValue | |
urlRequest.addValue(MY_API_KEY, forHTTPHeaderField: "X-Mashape-Key") | |
urlRequest.addValue("application/json", forHTTPHeaderField: "Accept") | |
Alamofire.request(urlRequest) | |
.responseJSON { response in | |
debugPrint(response) | |
} | |
} | |
} | |
// Alternatively: | |
func doRequestWithHeaders4() { | |
if let url = URL(string: "https://mashape-community-urban-dictionary.p.mashape.com/define?term=smh") { | |
var urlRequest = URLRequest(url: url) | |
urlRequest.httpMethod = HTTPMethod.get.rawValue | |
var headers: HTTPHeaders | |
if let existingHeaders = urlRequest.allHTTPHeaderFields { | |
headers = existingHeaders | |
} else { | |
headers = HTTPHeaders() | |
} | |
headers["X-Mashape-Key"] = MY_API_KEY | |
headers["Accept"] = "application/json" | |
urlRequest.allHTTPHeaderFields = headers | |
let request = Alamofire.request(urlRequest) | |
.responseJSON { response in | |
debugPrint(response) | |
} | |
debugPrint(request) | |
} | |
} | |
// MARK: - Use an Alamofire RequestAdapter to add headers to all calls in the session | |
class MashapeHeadersAdapter: RequestAdapter { | |
func adapt(_ urlRequest: URLRequest) throws -> URLRequest { | |
var urlRequest = urlRequest | |
urlRequest.setValue(MY_API_KEY, forHTTPHeaderField: "X-Mashape-Key") | |
urlRequest.setValue("application/json", forHTTPHeaderField: "Accept") | |
return urlRequest | |
} | |
} | |
func doRequestWithHeaders5() { | |
// Then to make the call: | |
// get a session manager and add the request adapter | |
let sessionManager = Alamofire.SessionManager.default | |
sessionManager.adapter = MashapeHeadersAdapter() | |
// make calls with the session manager | |
let request = sessionManager.request("https://mashape-community-urban-dictionary.p.mashape.com/define?term=smh") | |
.responseJSON { response in | |
debugPrint(response) | |
} | |
debugPrint(request) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment