Last active
November 9, 2019 17:27
-
-
Save wadheraswati/7252a39ef50260c9915c73f5a2715d67 to your computer and use it in GitHub Desktop.
Create URL Request with Parameters
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
func getURLRequest(method: HTTPMethod, body: Encodable? = nil, params: [String: Any]? = nil) throws -> URLRequest { | |
do { | |
var queryParams = [String: Any]() | |
if let myParams = params { | |
queryParams = myParams | |
} | |
queryParams[Constants.token] = AppCache.token | |
let urlString = self.appendParams(withQuery: queryParams) | |
let myURL = try urlString.asURL() | |
var urlRequest = URLRequest(url: myURL) | |
urlRequest.httpMethod = method.rawValue | |
urlRequest.addHeaders() | |
if let apiBody = body { | |
let encodedURLRequest = try URLEncoding.default.encode(urlRequest, with: apiBody.asJson()) | |
return encodedURLRequest | |
} | |
return urlRequest | |
} catch { | |
print("you may handle it the way you want") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment