How do I use JSON arrays with Alamofire parameters?

前端 未结 8 1977
借酒劲吻你
借酒劲吻你 2021-01-05 01:03

I\'m having a bit of trouble structuring my parameters so that our server API would be able to read it as valid JSON.

Alamofire uses parameters like this in swift la

8条回答
  •  旧时难觅i
    2021-01-05 01:22

    In case, there is a need to pass array directly as a parameter for a alamofire request, the following method worked for me.

    source: https://github.com/Alamofire/Alamofire/issues/1508

    let headers = NetworkManager.sharedInstance.headers
    var urlRequest = URLRequest(url: URL(string: (ServerURL + Api))!)
    urlRequest.httpMethod = "post"
    urlRequest.allHTTPHeaderFields = headers
    let jsonArrayencoding = JSONDocumentArrayEncoding(array: documents)
    let jsonAryEncodedRequest = try? jsonArrayencoding.encode(urlRequest, with: nil)
    
        var request: Alamofire.DataRequest? = customAlamofireManager.request(jsonAryEncodedRequest!)
        request?.validate{request, response, data in
            return .success
            }
            .responseJSON {
    

提交回复
热议问题