没有 url 编码的 Moya Alamofire 请求

Moya Alamofire request with no url coding

如何使用 Moya 使用 get-API(具有以下请求结构)?

http://some.environment.com/some/api/some/contacts/81193?types=["details", "permissions"]

这是我试过的方法。

enum MyApiTarget: TargetType {

    case getInfo(contactID: Int, types: [String])

    public var baseURL: URL {
        switch self {
        case .getInfo:
            return URL(string: "http://some.environment.com/some/api")!
        }
    }

    public var path: String {
        switch self {
        case .getInfo(contactID: let contactId, types: _):
            return "/some/contacts/\(contactId)"
        }
    }

    public var method: Moya.Method {
        switch self {
        case .getInfo:
            return .get
        }
    }

    public var sampleData: Data {
        return Data()
    }

    public var task: Task {
        switch self {
        case .getInfo(contactID: _, types: let types):
            return .requestParameters(
                 parameters: ["types" : types],
                 encoding: URLEncoding.queryString
            )
        }
    }

    public var headers: [String: String]? {
        return nil
    }

}

以上代码产生以下 URL.

http://some.environment.com/some/api/some/contacts/81193?types%5B%5D=details&types%5B%5D=permissions

我试过以下编码方法

None 的编码帮助我产生了预期的结果。

我不知道 MOYA

我遇到了类似的问题和类似的结构,我已经用

解决了这个问题
public protocol Router:URLRequestConvertible {
    var endPoint : String {get}
    var parameters:Parameters? {get}
    var httpMethod : HTTPMethod {get}
    var encoding:ParameterEncoding {get}
     func asURLRequest() throws -> URLRequest
}

现在我有了扩展,可以从包含的所有协议属性构建 URL encoding:ParameterEncoding

extension Router {
    func asURLRequest() throws -> URLRequest {

        // Generate URL
        let url = try Constants.APIConstants.BaseURL.production.rawValue.asURL()

        // Generate  URL Request
        var urlRequest = URLRequest(url: url.appendingPathComponent(endPoint))

        // Generate Headers

        urlRequest.addValue("application/json", forHTTPHeaderField:"Content-Type")
        urlRequest.addValue("application/json", forHTTPHeaderField: "Accept")
        urlRequest.addValue(Constants.APIConstants.RequestKeys.APIKEY_Val, forHTTPHeaderField:  Constants.APIConstants.RequestKeys.APIKEY)
        urlRequest.addValue(Constants.APIConstants.RequestKeys.APIVERSION_Val , forHTTPHeaderField:Constants.APIConstants.RequestKeys.APIVERSION )
        urlRequest.addValue(authToken, forHTTPHeaderField:Constants.APIConstants.RequestKeys.AUTHTOKEN )

        urlRequest.httpMethod = self.httpMethod.rawValue


        if let parameters = parameters {
            do {
                urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters, options:[])
                urlRequest =  try  self.encoding.encode(urlRequest, with: parameters)

            } catch {
                throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
            }
        }

        return urlRequest

    }
}

现在我有了枚举

enum APIRouterUserModule : Router {

    case login(email:String,password:String) 

     // Implement all the  Router protocol property (similar to your strct.) here is example for encoding 

            var encoding: ParameterEncoding {
                 switch self {
                     case .login :
                     return URLEncoding.queryString
                    //Return any of them 
                        /*
                   URLEncoding.queryString
                   URLEncoding.default
                   URLEncoding.httpBody
                   JSONEncoding.default
                   JSONEncoding.prettyPrinted */
                  }

             }
   }

希望对您有所帮助

我通过如下代码解决了它。

替换任务如下。

public var task: Task {
    switch self {
        case .getInfo(contactID: _, types: let types):
            var arrayOfTypesInString = types.joined(separator: "\", \"")
            arrayOfTypesInString = "\"\(arrayOfTypesInString)\""
            let params = ["types": "[\(arrayOfTypesInString)]"]
            return .requestParameters(
                 parameters: params,
                 encoding: URLEncoding.queryString
            )
        }
    }
}

目前,我已完成手动 JSON 编码。另一种方法是,首先将数据转换为 JSON 并从 JSON 创建字符串并提供。