IOS http POST using Alamofire

advertisements

I need some help using Alamofire to do a POST to a web page using a username/password throug an API from Twilio.

I have used SwiftRequest from GitHub before, but that doesn't support Swift 2.0.

The code I used (using SwiftRequest) was:

        var data = [
            "To" : mobileInput.text as String!,
            "From" : twilioSMSFrom,
            "Body" : String(code) as String
        ]

        var swiftRequest = SwiftRequest()

        swiftRequest.post("https://api.twilio.com/2010-04-01/Accounts/\(twilioUsername)/Messages",
            auth: ["username" : twilioUsername, "password" : twilioPassword],
            data: data,
            callback: {err, response, body in
                if err == nil {
                    println("Success: \(response)")
                } else {
                    println("Error: \(err)")
                }
            })

How can I translate this to use Alamofire instead?

I have tried searching for a solution, but could find any.

Can anybody help me?


Try something like this:

Alamofire.request(.POST, "https://api.twilio.com/2010-04-01/Accounts/\(twilioUsername)/Messages", parameters: ["username": twilioUsername, "password" : twilioPassword])
     .responseJSON { response in
         print(response.request)
         print(response.response)
         print(response.result)

         if let JSON = response.result.value {
             print("Did receive JSON data: \(JSON)")
         }
         else {
             print("JSON data is nil.")
         }

      }

You should definitely check their github page - https://github.com/Alamofire/Alamofire