带有参数的URLRequest的Almofire

时间:2018-04-05 09:18:14

标签: ios swift alamofire

我在我的应用程序中使用Almofire。如何在使用.get方法时使用字典。我的代码如下:

func validMemberId(completion: @escaping CompletionHandler){
    guard let cookie = self.cookies else {
        return
    }
    let isRegisterMemberUrl = createUrl(with: checkRegisterMemberIdUrl)

    var isRegisterMemberRequest = URLRequest(url: isRegisterMemberUrl)
    isRegisterMemberRequest.httpMethod = HTTPMethod.get.rawValue
    isRegisterMemberRequest.allHTTPHeaderFields = HTTPCookie.requestHeaderFields(with: cookie)
    Alamofire.request(isRegisterMemberRequest).responseString { (response) in
        { (response) in
            if response.result.isSuccess && response.response?.statusCode == 200 {
            }
        }
}

我想使用Get方法和URLRequest,我的词典[String: Any] = ["ID": "001110"]

传递此词典

2 个答案:

答案 0 :(得分:0)

参数应为Dictionary(键,值)

对于获取请求,您无需设置方法

let parameters: Parameters = ["foo": "bar"]

Alamofire.request("https://httpbin.org/get", parameters: parameters) // encoding defaults to `URLEncoding.default`

如果您想提出post请求,只需添加method

let parameters: Parameters = ["foo": "bar"]

Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters)

答案 1 :(得分:0)

<强> Manual Parameter Encoding of a URLRequest

可以在发出网络请求之外使用ParameterEncoding API。

let url = URL(string: "https://httpbin.org/get")!
var urlRequest = URLRequest(url: url)

let parameters: Parameters = ["foo": "bar"]
let encodedURLRequest = try URLEncoding.queryString.encode(urlRequest, with: parameters)

Alamofire.request(encodedURLRequest)