0

im trying to get data from api using alamofire and it giving me invaildURL("http://tdlly.com/Api/Advertisements/1/18") i tried to encode url but also not working that's my code

 class func nearAds(completion: @escaping(_ error: Error?, _ data:[Ad]?)->Void) {
    let url = URLs.nearAds+"\(helper.getApiToken())"
    //let s  = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
    Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default , headers: nil).responseJSON { (response) in

        switch response.result
        {
        case.failure(let error):
            completion(error,nil)
            print("error for nearAds",error)
        case.success(let value):
            let json = JSON(value)
            print("near,\(json)")
            guard let dataArr = json.array else{
                completion(nil , nil)
                return
            }
            var ads = [Ad]()
            for data in dataArr {
                if let data = data.dictionary ,let result = Ad.init(dic: data) {
                    ads.append(result)
                }
            }
            completion(nil,ads)
        }
    }
}
  • Is the return value of helper.getApiToken() an optional? If so, your url string will consist of "Optional("")". – koropok Dec 06 '18 at 00:53
  • @koropok i save the token from login json at userDefaults and return it by this func class func getApiToken() -> Int { let def = UserDefaults.standard return (def.object(forKey: "user_id") as! Int) } – mahmoud AbdlAziz Dec 06 '18 at 00:58
  • i ran your URL on alamofire and it seems to be working. can you do a print on your url? – koropok Dec 06 '18 at 01:04
  • @koropok http://tdlly.com/Api/Advertisements/1/18 that is the url with token id when i try it at postman its working but at xcode not working give me invaildURL – mahmoud AbdlAziz Dec 06 '18 at 01:09
  • To debug your error, I'd make use of Postman. Throw a breakpoint in and type `po url` in the debugger. Then, in Postman, dump that url in from the debugger and see what the API kicks back. – Adrian Dec 06 '18 at 03:32
  • @Adrian i did already that at postman and it's already work fine without any problem – mahmoud AbdlAziz Dec 06 '18 at 03:34
  • @mahmoudAbdlAziz Whoever wrote that API returning all strings should be locked in a port-o-potty and lit on fire. That said, I'm able to hit that endpoint without the key method, which I presume is a string. Is the `helper.getApiToken()` an optional return? If so, you might want to unwrap the string from getApiToken before adding it to the end. What's `po url` print out if you throw a breakpoint into your method? If you can hit it in postman, it ought to be a valid url in the method, too. – Adrian Dec 06 '18 at 03:50
  • @Adrian i get the token from login then save it to userdefaults after that i call it by this class func getApiToken() -> Int { let def = UserDefaults.standard return (def.object(forKey: "user_id") as! Int) } – mahmoud AbdlAziz Dec 06 '18 at 03:53
  • @Adrian it i save just the num of user then return by that func to the url.but can u show me how to unwarp it safely ? – mahmoud AbdlAziz Dec 06 '18 at 03:58
  • i will try it right now and tell u the result – mahmoud AbdlAziz Dec 06 '18 at 04:01
  • @Adrian I tried ur way but give me this error Initializer for conditional binding must have Optional type, not 'Int' – mahmoud AbdlAziz Dec 06 '18 at 04:07
  • Post the method signature for `helper.getApiToken()`. I'm interested in what the return is. What you've posted tells me it's not optional. You've also indicated it's an Int. If it's an Int, you need to wrap it with `String(helper.getApiToken())`, so it would look like this: `URLs.nearAds+ String(helper.getApiToken())`. While we're at it, what does the declaration of `URLS.nearAds` look like? Is it an `enum`? If so, you might need to specify `.rawValue` on the end. – Adrian Dec 06 '18 at 04:14
  • i did that by convert the int to string with diffrent ways and still not work. and i didnt understand what u meant at last part but if u talk about the struct to init data its class object using SwiftyJSON . BUT thats not the problem im sure its at the request method but idk how to fix hhh – mahmoud AbdlAziz Dec 06 '18 at 04:20
  • The problem is your URL, so your problem likely lives in 1) `URLs.nearAds` and 2) `helper.getApiToken()`. You should post code for both of those declarations. Need to know what URL.nearAds looks like and what `helper.getApiToken()` returns. – Adrian Dec 06 '18 at 13:31
  • @Adrian yeah i did that too but this api for ads and the logout only have this issue but for other apis works so perfect even at postman its work .please can you try my code and seeif it will work with u or not ? – mahmoud AbdlAziz Dec 06 '18 at 16:09

1 Answers1

-1

Try to add http protocol to string. so for e.g. you link will looks like http://tdlly.com/Api/Advertisements/1/18

if you already did so please show screenshot of debug print (po url) after line

let url = URLs.nearAds+"\(helper.getApiToken())"

and make sure that you enabled NSExceptionAllowsInsecureHTTPLoads in Info.plist

check here Transport security has blocked a cleartext HTTP