Last active
May 27, 2019 05:24
-
-
Save MaatheusGois/c46a36247bd2ada61a9a0fdf812024fa to your computer and use it in GitHub Desktop.
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
//Não esqueca de importar o Foundation | |
func postRequest(url: String, params: [String: String], | |
completion: @escaping ([String: Any]?, Error?) -> Void){ | |
//URL válida | |
guard let URL = URL(string: url) else { | |
completion(nil, nil) | |
return | |
} | |
//Cria a representacão da requisição | |
let request = NSMutableURLRequest(url: URL) | |
//Converte as chaves em valores pares para os parametros em formato de String | |
let postString = params.map { "\($0.0)=\($0.1)" }.joined(separator: "&") | |
//Atribui à requisiçāo o método POST | |
request.httpMethod = "POST" | |
//Codifica o corpo da mensagem em "data" usando utf8 | |
request.httpBody = postString.data(using: String.Encoding.utf8) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment