Hello,
Is it possible to add x-www-form-urlencoded params in URL Component (for example with QueryItem) ?
I am trying to do a sign in / sign up form and I want the username and the password to be x-www-form-urlencoded.
What I currently have :
func signIn(for username : String, for password : String, completion: ((Result<User>) -> Void)?) {
urlComponents.path = "/users/signin/"
urlComponents.queryItems = [userIdItem]
guard let url = urlComponents.url else { fatalError("Could not create URL from components") }
var request = URLRequest(url: url)
request.httpMethod = "GET"
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: request) { (responseData, response, responseError) in
DispatchQueue.main.async {
if let error = responseError {
completion?(.failure(error))
} else if let jsonData = responseData {
let decoder = JSONDecoder()
do {
let user = try decoder.decode(User.self, from: jsonData)
completion?(.success(user))
} catch {
completion?(.failure(error))
}
} else {
let error = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey : "Data was not retrieved from request"]) as Error
completion?(.failure(error))
}
}
}
task.resume()
}
Thank you a lot