vidalbenjoe vidalbenjoe - 1 year ago 211
Swift Question

How to send request in Alamofire with parameters and headers using POST method in swift

I have a problem with sending a request using

I want to send a POST request with parameters, but I don't know where to put custom header like token.

Here is my code:

let parameters = [
"id": ID,
"recipient_id" : recipientID,
"is_match" : "1"

Alamofire.request(.POST, Constants.baseURL + Constants.apiURL + Constants.accept, parameters: parameters, encoding: .JSON)
.response { request, response, data, error in
.responseJSON { response in
switch response.result {
case .Success:
print("Approve Successful")
print("approve \(response)")

case .Failure(let error):

Btw I am using
Alamofire 3.4

Answer Source

Alamofire 4.0

let headers = ["Content-Type":"Application/json"]

Alamofire.request(requestString, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
                        print("Request  \(response.request)")

                    print("RESPONSE \(response.result.value)")
                    print("RESPONSE \(response.result)")
                    print("RESPONSE \(response)")

                        switch response.result {
                        case .success:

                        case .failure(let error):


in 3.0 u can also add headers like this . In parameters to func

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download