I try to call to Google Places
Api using Moya
and have a problem with URL. Maya
change characters in my URL. In this case for example before character ?
adds %3f
and change ,
for %2C
. When I copy and paste this address into my web browser, I receive an error, but when I delete %3f
and change and %2C
on ,
I receive a correct answer form API. What should I set in Moya
if I don't want to change this characters in my url?
my Moya
provider looks like that:
extension GooglePlacesService: TargetType {
var baseURL: URL {
return URL(string: "https://maps.googleapis.com")!
}
var path: String {
switch self {
case .gasStation:
return "/maps/api/place/nearbysearch/json?"
}
}
var parameters: [String : Any]? {
switch self {
case .gasStation(let lat, let long, let type):
return ["location": "\(lat),\(long)", "type" : "gas_station", "rankby" : "distance", "keyword" : "\(type)", "key" : GoogleKeys.googlePlacesKey]
}
}
var parameterEncoding: ParameterEncoding {
switch self {
case .gasStation:
return URLEncoding.queryString
}
}
var method: Moya.Method {
switch self {
case .gasStation:
return .get
}
}
var sampleData: Data {
switch self {
case .gasStation:
return "".utf8Encoded
}
}
var task: Task {
switch self {
case .gasStation:
return .request
}
}
}
private extension String {
var urlEscaped: String {
return self.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
}
var utf8Encoded: Data {
return self.data(using: .utf8)!
}
}
URL which generates Moya looks like that (doesn't work with API):
https://maps.googleapis.com/maps/api/place/nearbysearch/json%3F?key=MYAPIKEY&keyword=XXXXXX&location=51.0910166687869%2C17.0157277622482&rankby=distance&type=gas_station
URL which works with API:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=MYAPIKEY&keyword=XXXXXX&location=51.0910166687869,17.0157277622482&rankby=distance&type=gas_station