vidalbenjoe vidalbenjoe - 1 month ago 45
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

Alamofire
.
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)
.validate()
.response { request, response, data, error in
print(request)
print(response)
print(data)
print(error)
}
.responseJSON { response in
switch response.result {
case .Success:
print("Approve Successful")
print("approve \(response)")


case .Failure(let error):
print(error)
}
}


Btw I am using
Alamofire 3.4

Answer

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