Forked from cmoulton/Custom HTTP Headers with Swift and Alamofire.swift
Last active
November 1, 2016 23:41
-
-
Save artem-sherbachuk/863fe3808a4675aa555f63be5c7002d7 to your computer and use it in GitHub Desktop.
Custom HTTP Headers with Swift 3.0 and Alamofire 4.0: See https://grokswift.com/custom-headers-alamofire4-swift3/ for explanations
This file contains hidden or 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 | |
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 | |
// get the default headers | |
var headers = Alamofire.SessionManager.defaultHTTPHeaders | |
// add your custom header | |
headers["API-Version"] = "2.0" | |
// create a custom session configuration | |
let configuration = URLSessionConfiguration.default | |
// add the headers | |
configuration.httpAdditionalHeaders = headers | |
// create a session manager with the configuration | |
let 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 | |
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: | |
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 | |
} | |
} | |
// 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