Swift 使用Alamofire的iOS http POST
我需要一些帮助,使用Alamofire通过Twilio的API使用用户名/密码发布到网页 我以前使用过GitHub的SwiftRequest,但它不支持Swift 2.0 我使用的代码(使用SwiftRequest)是: 我如何将其转换为使用Alamofire 我试着寻找一个解决方案,但没有找到Swift 使用Alamofire的iOS http POST,swift,alamofire,Swift,Alamofire,我需要一些帮助,使用Alamofire通过Twilio的API使用用户名/密码发布到网页 我以前使用过GitHub的SwiftRequest,但它不支持Swift 2.0 我使用的代码(使用SwiftRequest)是: 我如何将其转换为使用Alamofire 我试着寻找一个解决方案,但没有找到 有人能帮我吗?试试这样的方法: Alamofire.request(.POST, "https://api.twilio.com/2010-04-01/Accounts/\(twilioUsername
有人能帮我吗?试试这样的方法:
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.")
}
}
你一定要查看他们的github页面-我发现了
使用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)
}
这是SWIFT 2.2版本的最新答案 试试这个,它能帮你 参数:-
let params : Dictionary = ["YourKEY" : "YourVALUE"]
邮寄申请表格:-
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)")
}
}
这返回了成功,但您的答案不包括数据部分。我如何确保数据也被发送?很抱歉,不够精确。我的意思是数据部分必须在POST中发送,以便能够在结果中获得必要的数据。我已经测试了您的代码,它的响应是JSON数据为零,正如预期的那样。
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)")
}
}