1

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")
      }
   }
Ryan Kanno
  • 105
  • 8
  • 3
    What's the actual value of `urlString`? – MadProgrammer Sep 13 '21 at 00:10
  • 1
    Post the `urlString`. It is most likely a formatting issue with you supply that. – zaitsman Sep 13 '21 at 00:10
  • https://stackoverflow.com/a/49492609/6576315 – RTXGamer Sep 13 '21 at 00:11
  • 1
    @RTXGamer You should provide some description for what this link is for. E.g. is this a duplicate question? A similar solution? A general reference for how to do something? Etc, would be useful for some description. – George Sep 13 '21 at 01:21
  • what is the parameter `returnType: T.Type` for? After `print("\n\(#function)")` could you put `print(urlString)` and show us what it prints. – workingdog support Ukraine Sep 13 '21 at 01:39
  • @MadProgrammer my exact url is: https://dev.virtualearth.net/REST/v1/TimeZone/?key={MY-API-KEY}&query=tokyo, japan&output=JSON It works perfectly fine in Postman and in any browser, but not in app. I double checked the url is formatted correctly, and copy pasted the url from the app when I was stepping through it to make sure that exact url works in Postman and in browser. – Ryan Kanno Sep 13 '21 at 13:44
  • 1
    remove the space between tokyo, and japan" like this "...query=tokyo,japan&output=JSON" – workingdog support Ukraine Sep 13 '21 at 14:08
  • @workingdog Thank you, that was the problem. I guess Postman and browsers don't care if there's a space in the url, but Swift does. – Ryan Kanno Sep 14 '21 at 17:24

1 Answers1

0

There was a space in my url that Swift wasn't accepting. Apparently Postman and browsers can still make the call with a space, but not Swift. Thank you to @workingdog for solving this.

Ryan Kanno
  • 105
  • 8