简体   繁体   中英

Is it possible to send Array and Dictionary both into parameters at api hitting time using Alamofire swift?

I having the values like following.

let values = ["06786984572365", "06644857247565", "06649998782227"]
let params:[String:Any] = ["delete_wishlist_products":"1"]

Is any possibule way for sending both dictionay and array as a params with out headers, at API hitting time in Alamofire by using Swifft.

find my sample code here.

 func deleteWishlistValue(){

        let values = ["12345", "1345", "1234"]
        let params:[String:Any] = ["delete_wishlist_products":"1",
                                   "product_id":values,
                                   "customer_id":"152698"]

        Alamofire.request(Services.deleteWishlist,method: .get,parameters: params, headers:nil).responseJSON { (response) in
                            if response.result.error == nil{
                                print(response)
                            }else{
                                print(response.result.error?.localizedDescription ?? "")
                            }
        }
    }

Here is an example how to send array data in api using almofire

func getContest(arrDetails: [String],  completion: @escaping(_ reponse: ContestListResponse?, _ error: ApiError?)-> Void) {
    contestListRequest?.cancel()
    let parameters: Parameters = [
        "limit": "10",
        "details": arrDetails
        ]
let headers: HTTPHeaders = [
        "language": DBManager.getUserData()?.language ?? "eng"
    ]
    contestListRequest = Alamofire.request(AppURL.contestList, method:.post, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
        .responseContestListResponse(completionHandler: { (response) in
            switch response.result {
            case .success(let result):
                print("result---",result)
                if response.response?.statusCode == 200 {
                    completion(result, nil);
                } else {
                    completion(result, ApiError(domain: .api, code: response.response?.statusCode ?? 0, msg: result.message ?? ""))
                }
            case .failure(let error):
                completion(nil, ApiError(domain: .system, code: error._code, msg: error.localizedDescription))
            }
        })
        .responseString { (response) in
            print(response.result.value ?? "NO Response")
    }
}

Change the response class and completion handler yourself.

Notice the encoding: JSONEncoding.default this is required from you for sending array or dict directly to parameters, ask your backend dev to get the properties accordingly.

Yes You just need to define an array, you are almost there, just need an extra set of [] in your dictionary. :

let values = [["product_id" : 06786984572365, "option_id" : 1]]
let params: [String: Any] = [
        "delete_wishlist_products":"1",
        "array":values,
        "customer_id":"152698"
    ]

Alamofire.request(Services.deleteWishlist,method: .get,parameters: params, headers:nil).responseJSON { (response) in
                            if response.result.error == nil{
                                print(response)
                            }else{
                                print(response.result.error?.localizedDescription ?? "")
                            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM