Moya / Alamofire

编程入门 行业动态 更新时间:2024-10-14 22:23:09
本文介绍了Moya / Alamofire - 具有相同键的URL编码参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 Moya Swift框架来构建基于 Alamofire 。

I'm using Moya Swift framework for networking layer which is constructed on top of Alamofire.

目前,我正在尝试发送请求具有相同键的URL编码参数。

Currently, I'm trying to send request with URL encoded parameters that have same keys.

ie http:// some-site / request?param = v1& param = v2& param = v3

I我们已经尝试将这些参数分组为Set或NSSet或Array,但没有任何帮助可以达到预期的结果。

I've already tried to group these params into Set or NSSet or Array like this but nothing helps to achieve desired result.

[param: [v1,v2,v3]];

[param:Set( arrayLite:v1,v2,v3)]

任何帮助都可以通过Moya或Alamofire本身获得。

Any help would be appreciated either with Moya or with Alamofire itself.

编辑:下面是一些示例代码,提供基本想法:

Here is some sample code to give basic idea:

Api路由器设置

import Moya // MARK:- Enum Declaration enum ApiRouter { case XAuth(login: String, password: String) case SomeRequest(params: [String]) } // MARK:- Moya Path extension ApiRouter: MoyaPath { var path: String { switch self { case .XAuth: return "/authorization" case .SomeRequest: return "/some-request" } } } // MARK:- Moya Target extension ApiRouter: MoyaTarget { private var base: String { return "some-site" } var baseURL: NSURL { return NSURL(string: base)! } var parameters: [String: AnyObject] { switch self { case .XAuth(let login, let password): return [ "email": login, "password": password ] case .SomeRequest(let params): return [ "params": params ] } var method: Moya.Method { switch self { case .XAuth: return .POST case .SomeRequest, return .GET } } var sampleData: NSData { switch self { case .XAuth: return "{}".dataUsingEncoding(NSUTF8StringEncoding) case .ServiceRequests: return "{}".dataUsingEncoding(NSUTF8StringEncoding) } } }

Api提供商设置

let endpointsClosure = { (target: ApiRouter) -> Endpoint<ApiRouter> in let endpoint = Endpoint<ApiRouter>( URL: target.baseURL.URLByAppendingPathComponent(target.path).absoluteString!, sampleResponse: EndpointSampleResponse.Success(200, { target.sampleData }), method: target.method, parameters: target.parameters, parameterEncoding: parameterEncoding(target) ) switch target { case .XAuth: return endpoint default: let token = "some-token" return endpoint.endpointByAddingHTTPHeaderFields(["Authorization": "Bearer: \(token)"]) } } func parameterEncoding(target: ApiRouter) -> Moya.ParameterEncoding { switch target { case .XAuth: return .JSON case .SomeRequest: return .URL } } let apiProvider = MoyaProvider(endpointsClosure: endpointsClosure) apiProvider.request(ApiRouter.SomeRequest(params: ["v1", "v2", "v3"], completion: { (data, statusCode, response, error) in /* ... */ })

谢谢。

推荐答案

所以我找到了一个非常简单明了的解决方案。阅读 Alamofire 的文档,我发现了这一点:

So I found a solution which is actually pretty simple and obvious. Reading Alamofire's documentation I found this:

由于没有关于如何编码集合类型的已发布规范,Alamofire遵循将[]附加到数组值的键的约定(foo [] = 1& foo [] = 2) ,并附加方括号包围的键,用于嵌套字典值(foo [ba r] = baz)。

Since there is no published specification for how to encode collection types, Alamofire follows the convention of appending [] to the key for array values (foo[]=1&foo[]=2), and appending the key surrounded by square brackets for nested dictionary values (foo[bar]=baz).

因此,对于这种情况,自定义 ParameterEncoding 选项,它可以实际指定您希望如何形成参数的实现。

So, for this cases there's Custom ParameterEncoding option which takes closure where you can actually specify your own implementation of how you want parameters to be formed.

这里的答案是同一个问题。

Here's the same question with the same answer.

更多推荐

Moya / Alamofire

本文发布于:2023-11-26 02:20:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632264.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Moya   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!