I have a url that works in Postman and in browser, but not in app. I have an if let url = URL(string: urlString) line, but apparently the URL(string: urlString) is returning nil so it doesn't enter the block. It doesn't actually throw an error so I can't actually search for an error.
I've tried looking at other people's similar problems but haven't found a solution. Any help would be appreciated. If you could point me to another post with a potential solution I'd appreciate that too. Thank you.
Here is my code. I've used this many times before with no problems.
func performRequest<T: Codable>(urlString: String, returnType: T.Type, completion: @escaping (Result<T, Error>) -> Void ) {
print("\n\(#function)")
if let url = URL(string: urlString) { // <--- FAILS RIGHT HERE
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url) { (data, _, error) in
if let error = error {
completion(.failure(error))
return
}
guard let data = data else { return }
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(T.self, from: data)
completion(.success(decodedData))
} catch let decodingErr {
completion(.failure(decodingErr))
}
}
task.resume()
} else {
print("Something went wrong with the url")
}
}