I'm trying to use Moya with RxSwift in my project
I'm facing the problem with url contain "?"
This TargetType I have create
private extension String {
var URLEscapedString: String {
return self.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlHostAllowed)!
}
}
enum TMDb {
case discoverMovieList(page: Int)
case discoverMovieDetail(moive: Movie)
}
extension TMDb: TargetType {
var baseURL: URL { return URL(string: BASE_URL)! }
var path: String {
switch self {
case .discoverMovieList(page: let page):
return "discover/movie?api_key=\(API_KEY)&sort_by=release_date.desc&page=\(page)"
}
}
var method: Moya.Method {
return .get
}
var parameters: [String: Any]? {
return nil
}
var sampleData: Data {
switch self {
case .discoverMovieList(page: _):
return "test".data(using: .utf8)!
case .discoverMovieDetail(moive: _):
return "test1".data(using: .utf8)!
}
}
var task: Task {
return .request
}
var parameterEncoding: ParameterEncoding {
return URLEncoding.default
}
}
The problem is when I make the request. The url path return is somehow not correct
This is the url i got from console
Optional("https://api.themoviedb.org/3/discover/movie%3Fapi_key=58da429caf2e25e8ff9436665e2f0e36&sort_by=release_date.desc&page=1")
But the correct one should be
https://api.themoviedb.org/3/discover/movie?api_key=58da429caf2e25e8ff9436665e2f0e36&sort_by=release_date.desc&page=1
There something wrong when handle the "?" (it become %3F" character in the url. How can we make it work normally?
Update
This is how I call my Moya
let provider: RxMoyaProvider<TMDb>
let persistentContainer: PersistentContainer
func discoverMoiveList(for page: Int) {
self.provider.request(TMDb.discoverMovieList(page: 1)) { (result) in
print(result.value?.request?.url?.absoluteString ?? "no url")
}
}
}
The problem is not about "?". It occurred because of you used optional values in your path URL. Control BASE_URL and API_KEY, they must not be optional values.
There is no problem with the Moya. Here Moya-Logger result for your provider:
Here you've got a topic how to get an optional variable without
Opt()
. I prefer that way where you create anextension
:You should pass anything after the ? as parameter, this way Moya knows how to construct the URL correctly.