Reputation: 97
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?
Upvotes: 2
Views: 4854
Reputation: 557
This is latest answer of SWIFT 2.2 VERSION try this one its helps you....
PARAMETERS:-
let params : Dictionary = ["YourKEY" : "YourVALUE"]
Post Request_Form:-
Alamofire.request(.POST,"Post Your Url HERE", parameters: params, encoding:.JSON).responseJSON
{
response in switch response.result
{
case .Success(let JSON):
// print("Success with JSON: \(JSON)")
//converting json into NSDictionary
let response = JSON as! NSDictionary
print(response)
var array = NSMutableArray!()
//converting respose form into NSMutableArray formate
array = response.valueForKey("countryList")as? NSMutableArray
//example if there is an id
// let userId = response.objectForKey("id")!
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
Upvotes: 0
Reputation: 97
I figured it out.
The solution using Alamofire:
let data = [
"To" : mobileInput.text as String!,
"From" : twilioSMSFrom,
"Body" : String(code) as String
]
Alamofire.request(.POST, "https://\(twilioUsername):\(twilioPassword)@api.twilio.com/2010-04-01/Accounts/\(twilioUsername)/Messages", parameters: data)
.responseJSON { response in
print(response.request)
print(response.response)
print(response.result)
}
Upvotes: 1
Reputation: 395
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
Upvotes: 1