Search code examples
getalamofireios11

Invalid URL Error in alamofire in IOS while passing right url


What I want to do is to get json data from below url

https://query.yahooapis.com/v1/public/yql?q=select * from 
   weather.forecast where woeid in (select woeid from geo.places(1) where 
   text='seoul,kr') and u='c'&format=json

which works great if type it on browser.

but it dosen't work on alamofire... I think it's because of ' mark as I can see \ is embedded saying FATAL invalid url

let rrrr="https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='seoul,kr') and u='c'&format=json"
        print(rrrr)
        let alamo=Alamofire.request(rrrr,method:.get)
        alamo.responseJSON {

            response in
            if response.data != nil {
                print("not null")
                let json = JSON(data: response.data!)
                print(json)
                print("not null21")
                print(response)
                print("not null12")
                print(response.data!)

Log result is as follows

https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='seoul,kr') and u='c'&format=json
not null
null
not null21
FAILURE: invalidURL("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\'seoul,kr\') and u=\'c\'&format=json")
not null12

Solution

  • As I have seen your URL string contains the spaces in select query.

    Actually, alamofire doesn't support URL string with spaces. Try to replace the URL string spaces with %20 (i.e encoded character space).

    OR you can use URL encoding with the Alamofire request.

    Hope it will help!