Swift: Mandrill POST 问题
Swift: Mandrill POST Issue
我正在尝试通过 Mandrill API 发送电子邮件。
我在下面找到 swift 代码 here 用于 JSON HTTP POST 方法:
func createRequest(myUrl : String, type : String, params : NSDictionary?, completion : (AnyObject?, NSError?)->Void ){
var url = NSURL(string: myUrl)
var request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: 60.0)
if params != nil {
var data = NSJSONSerialization.dataWithJSONObject(params!, options: NSJSONWritingOptions.PrettyPrinted, error: nil)
request.setValue("\(data!.length)", forHTTPHeaderField: "Content-Length")
request.HTTPBody = data
}
request.setValue("application/json", forHTTPHeaderField:"Content-type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.HTTPMethod = type
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(),
completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) in
var returnedObject : AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableLeaves, error: nil)
completion(returnedObject,error)
})
}
现在我这样设置 url 和参数:
let url = "https://mandrillapp.com/api/1.0/messages/send.json"
let to = ["email" : "xyz@gmail.com"]
let message = [
"from_email" : "noreply@abc.com",
"from_name" : "abc",
"subject" : "Message through abc",
"text" : "sample message",
"to" : to
]
let params = [
"key": "myKey",
"message" : message
]
然后我这样调用这个函数:
createRequest(url, type: "POST", params: params, completion: {(returnedObject : AnyObject?, error : NSError?)in
if (returnedObject != nil) {
println("data = \(returnedObject)")
}
})
但我得到错误:
data = Optional({
code = "-2";
message = "Validation error: {\"message\":{\"to\":[\"Please enter an array\"]}}";
name = ValidationError;
status = error;
})
我做错了什么?
我不知道你用的是什么框架(甚至编程语言),但是回复好像很清楚。
事情是这样的:
let to = ["email" : "xyz@gmail.com"]
这对我来说显然不像数组,更像是字典(或语言中的等效项)。我认为以下方法可行:
let to = ["xyz@gmail.com"]
编辑
也许,这会:
"to": [
{
"email": "recipient.email@example.com",
"name": "Recipient Name",
"type": "to"
}
]
我的下一个猜测是名称和类型不是可选的..并且 "to" 字段需要一个字典项数组。
终于!!!我自己想出来了。
let to = ["email" : "kashif.izhar@gmail.com"]
应该是
let to = [["email" : "kashif.izhar@gmail.com"]]
希望对大家有所帮助
我正在尝试通过 Mandrill API 发送电子邮件。 我在下面找到 swift 代码 here 用于 JSON HTTP POST 方法:
func createRequest(myUrl : String, type : String, params : NSDictionary?, completion : (AnyObject?, NSError?)->Void ){
var url = NSURL(string: myUrl)
var request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: 60.0)
if params != nil {
var data = NSJSONSerialization.dataWithJSONObject(params!, options: NSJSONWritingOptions.PrettyPrinted, error: nil)
request.setValue("\(data!.length)", forHTTPHeaderField: "Content-Length")
request.HTTPBody = data
}
request.setValue("application/json", forHTTPHeaderField:"Content-type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.HTTPMethod = type
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(),
completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) in
var returnedObject : AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableLeaves, error: nil)
completion(returnedObject,error)
})
}
现在我这样设置 url 和参数:
let url = "https://mandrillapp.com/api/1.0/messages/send.json"
let to = ["email" : "xyz@gmail.com"]
let message = [
"from_email" : "noreply@abc.com",
"from_name" : "abc",
"subject" : "Message through abc",
"text" : "sample message",
"to" : to
]
let params = [
"key": "myKey",
"message" : message
]
然后我这样调用这个函数:
createRequest(url, type: "POST", params: params, completion: {(returnedObject : AnyObject?, error : NSError?)in
if (returnedObject != nil) {
println("data = \(returnedObject)")
}
})
但我得到错误:
data = Optional({
code = "-2";
message = "Validation error: {\"message\":{\"to\":[\"Please enter an array\"]}}";
name = ValidationError;
status = error;
})
我做错了什么?
我不知道你用的是什么框架(甚至编程语言),但是回复好像很清楚。
事情是这样的:
let to = ["email" : "xyz@gmail.com"]
这对我来说显然不像数组,更像是字典(或语言中的等效项)。我认为以下方法可行:
let to = ["xyz@gmail.com"]
编辑
也许,这会:
"to": [ { "email": "recipient.email@example.com", "name": "Recipient Name", "type": "to" } ]
我的下一个猜测是名称和类型不是可选的..并且 "to" 字段需要一个字典项数组。
终于!!!我自己想出来了。
let to = ["email" : "kashif.izhar@gmail.com"]
应该是
let to = [["email" : "kashif.izhar@gmail.com"]]
希望对大家有所帮助