like this,
var task: Task {
switch self {
case .xxxrequest(let IDs): // IDs is a Int array
return .requestParameters(parameters: ["ids" : IDs], encoding: URLEncoding.queryString)
}
}
then I got a 400HTTP code, and I retry the JSONEncoding and PropertyListEncoding, but still not work.
The expected result like this:
http://www.domain.com/xxx/xxx?ids=1&ids=2
Help! Please
Hey @canny09!
Can you post what result do you got when using code you included (with URLEncoding.querystring)? The best way would be to add NetworkLoggerPlugin to your plugins in MoyaProvider and copy whatever it prints (both request/response logs). More info about plugin customization you can find here.
@sunshinejr
Hey. Thanks for comment.
I add the default NetworkLoggerPlugin in provider, and then log like this:
"Moya_Logger: [18/01/2018 00:27:40] HTTP Request Method: PUT"]
["Moya_Logger: [18/01/2018 00:27:40] Response: <NSHTTPURLResponse: 0x17403d820> { URL: http://xxx.xxx.xxx.xxx/shop/api/v1/shop/decorations/buy?ids%5B%5D=127&ids%5B%5D=135 }
{ status code: 400, headers
{Connection = close;
"Content-Type" = "application/json;charset=UTF-8";
Date = "Wed, 17 Jan 2018 16:27:40 GMT";
"Transfer-Encoding" = Identity;
"X-Application-Context" = "gateway-service:default:8899";
}
}]
Error: ["status": 400, "exception": org.springframework.web.bind.MissingServletRequestParameterException,
"message": Required List parameter 'ids' is not present,
"timestamp": 1516206460196,
"path": /api/v1/shop/decorations/buy,
"error": Bad Request]
It looks like Moya encode the parameter to ids[]=127, but own server not parse it. So, how do remove the []character? just like ids=127&ids=135?
@canny09 I believe this is not supported. Additionally, with your syntax, few of the server-side languages would only catch one ids, because you passed two arguments for one parameter. If you still need it though (can't talk with the server guys), you probably need to write your own ParameterEncoding (here you have a quick example on how to create your custom one).
@sunshinejr
Thanks. After reading the source code of URLEncoding, I write(just copy and modify simply the source code -_-) a custom encoding URLQueryArrayEncoding, the code is follow:
// QueryArrayEncoding.swift
import Foundation
import Alamofire
import CoreFoundation
extension NSNumber {
fileprivate var isBool: Bool { return CFBooleanGetTypeID() == CFGetTypeID(self) }
}
struct QueryArrayEncoding: ParameterEncoding {
public static var `default`: QueryArrayEncoding { return QueryArrayEncoding() }
func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var request = try urlRequest.asURLRequest()
guard let parameters = parameters else {return request}
guard let url = request.url else {
throw AFError.parameterEncodingFailed(reason: .missingURL)
}
if var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false), !parameters.isEmpty {
let percentEncodedQuery = (urlComponents.percentEncodedQuery.map { $0 + "&" } ?? "") + query(parameters)
urlComponents.percentEncodedQuery = percentEncodedQuery
request.url = urlComponents.url
}
return request
}
private func query(_ parameters: [String: Any]) -> String {
var components: [(String, String)] = []
for key in parameters.keys.sorted(by: <) {
let value = parameters[key]!
components += queryComponents(fromKey: key, value: value)
}
return components.map { "\($0)=\($1)" }.joined(separator: "&")
}
private func queryComponents(fromKey key: String, value: Any) -> [(String, String)] {
var components: [(String, String)] = []
if let array = value as? [Any] {
for value in array {
components += queryComponents(fromKey: key, value: value)
}
} else if let value = value as? NSNumber {
if value.isBool {
components.append((key, value.boolValue ? "1" : "0"))
} else {
components.append((key, "\(value)"))
}
} else {
components.append((key, "\(value)"))
}
return components
}
}
It works for me now. But I think there are more elegant solutions. 馃様
This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
This issue has been auto-closed because there hasn't been any activity for at least 21 days. However, we really appreciate your contribution, so thank you for that! 馃檹 Also, feel free to open a new issue if you still experience this problem 馃憤.
@canny09
Hi, I have the same issue
and your solution is very helpful!
thank you馃槂
@canny09 , @applepeopletsai I have just copped with the same issue. The solution is simple. Standard library can remove brackets if you pass correct parameteres to encoder:
URLEncoding(arrayEncoding: .noBrackets)
if you want the same behavior as URLEncoding.queryString you can probably do like so:
URLEncoding(arrayEncoding: .noBrackets, destination: .queryString)
Most helpful comment
@sunshinejr
Thanks. After reading the source code of
URLEncoding, I write(just copy and modify simply the source code -_-)a custom encodingURLQueryArrayEncoding, the code is follow:It works for me now. But I think there are more elegant solutions. 馃様