Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire invalid URL

Getting error while trying to get info. Server supports russian language in URL.

Error:

[Result]: FAILURE: invalidURL(url: "http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=сплин&api_key=bad5acca27008a09709ccb2c0258003b&format=json")

Request:

Alamofire.request("http://ws.audioscrobbler.com/2.0/?    method=artist.search&artist=\(nameOfArtist)&api_key=bad5acca27008a09709ccb2c0258003b&format=json")
            .responseObject { (response: DataResponse<SearchArtistAPIModel>) in
                //to get status code
                debugPrint(response)
                if let status = response.response?.statusCode {

                    switch(status){

                    case 200...499:
                        print("example success")
                    default:
                        print("error with response status: \(status)")
                    }
                }

                if let artistName = response.result.value {

                    guard let artistsArray = artistName.results?.artistmatches?.artist else { return }
                    var names: [String] = []
                    for artists in artistsArray {
                        guard let artistsName = artists.name else { return }
                        names.append(artistsName)
                        completion(names)
                    }
                }
        }
like image 855
Ringo Ameyuri Avatar asked Jan 02 '23 07:01

Ringo Ameyuri


1 Answers

Encode your URL then create request.

let urlString = "http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=\(nameOfArtist)&api_key=bad5acca27008a09709ccb2c0258003b&format=json"        
if let encoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),let url = URL(string: encoded)
 {
     Alamofire.request(url).validate().responseJSON { (json) in
         print(json)
         //Enter your code here
     }
}
like image 86
Rocky Avatar answered Jan 15 '23 04:01

Rocky