Skip to content

Instantly share code, notes, and snippets.

@wadheraswati
Created November 9, 2019 17:28
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/b2bc650feb822749b3b0a85ad11ca4ea to your computer and use it in GitHub Desktop.
Save wadheraswati/b2bc650feb822749b3b0a85ad11ca4ea to your computer and use it in GitHub Desktop.
func getURLRequest(method: HTTPMethod, body: Encodable? = nil, params: [String: Any]? = nil) throws -> URLRequest {
do {
var queryParams = [String: Any]()
if let myParams = params {
queryParams = myParams
}
let myURL = try urlString.asURL()
var urlRequest = URLRequest(url: myURL)
urlRequest.httpMethod = method.rawValue
if let apiBody = body {
let encodedURLRequest = try URLEncoding.default.encode(urlRequest, with: apiBody.asJson())
return encodedURLRequest
}
return urlRequest
} catch {
// handle the exception here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment