2

I'm trying to make a post request with a body array in swift using Alamofire

postman like enter image description here

and here my code

class func storeEventContact(_ id:String, type_contact:String, user_id: Int, firstname:String, lastname: String,completionHandler:RequestCompletionHandler?){
    let url = "\(Endpoints.BASE)\(Endpoints.INVITE_STORE)"

    let params:NSMutableDictionary? = ["id": id,
                  "type_contact":type_contact,
                  "contacts": ["user_id" : user_id, "firstname" : firstname, "lastname" : lastname]]
    self.postRequest(url: url, parameters: params as? [String : Any]) { (result, error) in
        self.postRequest(url: url, parameters: params as? [String : Any]) { (result, error) in
            if error != nil{
                completionHandler?(result, error)
                return
            }
            let baseResponse = Mapper<BaseResponse>().map(JSONObject: result)
            if !baseResponse!.status{
                completionHandler?(baseResponse, error)
                return
            }
            completionHandler?(result, error)
        }
    }
}
1
  • what is the issue in passing array as parameter??? Commented Jan 15, 2018 at 8:28

1 Answer 1

7

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

let params = ["id": id,
              "type_contact":type_contact,
              "contacts": [
              [
                   "user_id": user_id, 
                   "firstname": firstname, 
                   "lastname": lastname
              ]]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.