New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using both URLEncoding and JSONEncoding #1059
Comments
Nvm, I found some inspiration from #909 and created a custom encoding. struct MultipleEncoding : ParameterEncoding {
var urlParameters: [String]?
init(urlParameters:[String]) {
self.urlParameters = urlParameters
}
func encode(_ urlRequest: Alamofire.URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
guard let parameters = parameters else { return urlRequest as! URLRequest }
//Encode URL Params
var filteredParams: [String: Any] = [:]
parameters.filter({ (key,value) in
return urlParameters?.contains(key) ?? false
}).forEach {
filteredParams[$0.0] = $0.1
}
let partiallyEncodedRequest = try URLEncoding.queryString.encode(urlRequest, with: filteredParams)
//Encode JSON
filteredParams = parameters.minus(dict: filteredParams)
return try JSONEncoding.default.encode(partiallyEncodedRequest, with: filteredParams)
}
} That's not very generic but I someone has the same issue, here is something to start with :) |
@amaurydavid parameter.minus is not working. And can you help out on how to use this? |
I no longer have access to that code but by the look of it, the minus function was just building a new dictionary with only entries that should be json encoded. Kinda the opposite of the filter above. |
I have to use an API that asks me to make a POST and put one parameter in the URL, others as JSON in the body.
If I had to only send the URL parameter, I would use URLEncoding.queryString
If I had to only send the body parameters, I would use JSONEncoding.default
But how can I send both with Moya? As far as I know, Moya only allow 1 parameter encoding.
As a workaround, in my implementation of TargetType I left the body params in
parameters
and 'parameterEncoding` to JSONEncoding.default, and tried to put the query parameter directly in the path, but doing so the '?' is encoded as '%3F' while it shouldn't and the API returns an error.Using Moya 8.0.3
The text was updated successfully, but these errors were encountered: