Created
September 13, 2017 00:38
-
-
Save arshbot/2a3b593941fa2bb5853d21cbcbd4e01f to your computer and use it in GitHub Desktop.
How to execute http request in swift
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
/* | |
PARAMS | |
URL - url of server + method wrapped in URL | |
httpMethod - For example: "GET", "POST", "PUT", "DELETE" etc | |
postData - body in string form. Be sure to include and escape quotations for string keys | |
auth - String key, string value dictionary. Should have "username" key and "password" key, will fail if not present | |
headers - header for request | |
completionHandler - block that is to be executed on the response. Block has three optional parameters of type Data, URLResponse, Error. | |
Returns void | |
*/ | |
func executeRequest(URL: URL, httpMethod: String, postData: String? = nil, auth: [String:String]? = nil, headers: [String: String] = ["content-type": "application/json", "cache-control": "no-cache"], completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void){ | |
var hdrs = headers | |
let request = NSMutableURLRequest(url: URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0) | |
if (postData != nil){ | |
request.httpBody = NSData(data: postData!.data(using: String.Encoding.utf8)!) as Data | |
} | |
if (auth != nil){ | |
let loginString = NSString(format: "%@:%@", auth!["username"]!, auth!["password"]!) | |
let loginData: NSData = loginString.data(using: String.Encoding.utf8.rawValue)! as NSData | |
let base64LoginString = loginData.base64EncodedString(options: []) | |
hdrs.updateValue("Basic \(base64LoginString)", forKey: "authorization") | |
} | |
request.httpMethod = httpMethod | |
request.allHTTPHeaderFields = hdrs | |
let session = URLSession.shared | |
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: completionHandler) | |
//Execute the call | |
dataTask.resume() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copy and paste to anywhere needed