pd jung
pd jung

Reputation: 59

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

Upvotes: 3

Views: 2959

Answers (2)

I use this:

enum ConsultOfficesServiceEndpoints : String {
    case getProposedPeople = "aaa/bbb/ccc/%d/"
}

.
.
.

func getProposedPeople(_ name: String, success: @escaping SuccessResponse,failure: @escaping FailureResponse){

    var paramWithSpaces = ""
    var lines = name.components(separatedBy: " ")
    var numlines = lines.count
    var i = 0
    if numlines > 1{
        while i < (numlines - 1) {
            paramWithSpaces = paramWithSpaces + lines[i] + "%20"
            i = i+1
        }
        paramWithSpaces = paramWithSpaces + lines[numlines - 1]
    }else{
        paramWithSpaces = name
    }

    self.GET(withEndpoint: ConsultOfficesServiceEndpoints.getProposedPeople.rawValue.replacingOccurrences(of: "%d", with: paramWithSpaces), params: nil, headers: nil, success: success, failure: failure)
}

Upvotes: 0

n....
n....

Reputation: 1285

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!

Upvotes: 4

Related Questions