Skip to content

Instantly share code, notes, and snippets.

@wadheraswati
Last active November 9, 2019 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wadheraswati/7252a39ef50260c9915c73f5a2715d67 to your computer and use it in GitHub Desktop.
Save wadheraswati/7252a39ef50260c9915c73f5a2715d67 to your computer and use it in GitHub Desktop.
Create URL Request with Parameters
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