将用户添加到 SWIFT 2 中的 MailChimp 订阅者列表
Add user to MailChimp subscriber list in SWIFT 2
我正在使用 MailChimp 进行电子邮件营销服务。现在我想将一个用户添加到我的订阅列表,但它显示无效电子邮件作为 JSON 响应。我的代码如下。
var url = "https://us4.api.mailchimp.com/2.0/lists/subscribe"
let request = NSMutableURLRequest(URL: NSURL(string: url)!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let params = ["apikey":"******************-us4", "id":"*******", "email":["email":email, "euid": "", "leid": ""], "merge_vars":["FNAME": name,"LNAME": ""], "double_optin": "false"]
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: [])
let task = session.dataTaskWithRequest(request) { data, response, error in
guard data != nil else {
print("no data found: \(error)")
return
}
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
print("Success: \(String(json))")
} catch let parseError {
print(parseError) // Log the error thrown by `JSONObjectWithData`
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Error could not parse JSON: '\(jsonStr)'")
}
}
task.resume()
我尝试了同样的方法,但与我的 mailchimp API 配合得很好。根据 mailchimp 官方文档 ( https://apidocs.mailchimp.com/api/2.0/lists/subscribe.php ),您的 json 格式是正确的。所以确保以下
1) 确保您的 API 密钥正确。
2) 确保您的列表 ID 正确。
3) 重新检查您的网络防火墙
4) 确保您的电子邮件不在当前列表中。
我正在使用 MailChimp 进行电子邮件营销服务。现在我想将一个用户添加到我的订阅列表,但它显示无效电子邮件作为 JSON 响应。我的代码如下。
var url = "https://us4.api.mailchimp.com/2.0/lists/subscribe"
let request = NSMutableURLRequest(URL: NSURL(string: url)!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let params = ["apikey":"******************-us4", "id":"*******", "email":["email":email, "euid": "", "leid": ""], "merge_vars":["FNAME": name,"LNAME": ""], "double_optin": "false"]
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: [])
let task = session.dataTaskWithRequest(request) { data, response, error in
guard data != nil else {
print("no data found: \(error)")
return
}
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
print("Success: \(String(json))")
} catch let parseError {
print(parseError) // Log the error thrown by `JSONObjectWithData`
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Error could not parse JSON: '\(jsonStr)'")
}
}
task.resume()
我尝试了同样的方法,但与我的 mailchimp API 配合得很好。根据 mailchimp 官方文档 ( https://apidocs.mailchimp.com/api/2.0/lists/subscribe.php ),您的 json 格式是正确的。所以确保以下
1) 确保您的 API 密钥正确。
2) 确保您的列表 ID 正确。
3) 重新检查您的网络防火墙
4) 确保您的电子邮件不在当前列表中。