GET petitions with ?
adomingd opened this issue · comments
Hello!
I'm testing this library (really nice btw) and I'm with some APIs from https://sampleapis.com/, but when I'm trying to get any list, the debug information prints a server error and the curl prints with a '?' at the final of the path.
Am I doing something wrong? Or why adds a '?' at the end?
This is my code:
class NetworkCall {
var network: NetworkingClient = {
var network = NetworkingClient(baseURL: "https://api.sampleapis.com")
network.logLevels = .debug
return network
}()
func getCoffees() -> AnyPublisher<Coffee, Error> {
network.get("/coffee/hot")
}
}
And the error:
sorry adomingd
I did not see your issue sooner.
There is an error in NetworkingRequest
private func getURLWithParams()
.
When there are no parameters for the url, an "empty" component is added,
resulting in having a "?" at the end of the url string.
To fix this in NetworkingRequest
use:
private func getURLWithParams() -> String {
let urlString = baseURL + route
guard let url = URL(string: urlString) else {
return urlString
}
// if no params, do not add any URLComponents
if params.isEmpty { return urlString } // <--- here
if var urlComponents = URLComponents(url: url ,resolvingAgainstBaseURL: false) {
var queryItems = urlComponents.queryItems ?? [URLQueryItem]()
params.forEach { param in
// arrayParam[] syntax
if let array = param.value as? [Any] {
array.forEach {
queryItems.append(URLQueryItem(name: "\(param.key)[]", value: "\($0)"))
}
}
queryItems.append(URLQueryItem(name: param.key, value: "\(param.value)"))
}
urlComponents.queryItems = queryItems
return urlComponents.url?.absoluteString ?? urlString
}
return urlString
}
@workingDog Thanks for the fix, pushing it ASAP
It's fixed now. Thank you much! @workingDog, @s4cha