1
我正在使用MailChimp進行電子郵件營銷服務。現在我想添加一個用戶到我的訂閱列表,但它說無效的電子郵件作爲JSON響應。我的代碼如下。將用戶添加到SWIFT 2中的MailChimp用戶列表
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()
感謝您的回答,我的列表ID存在問題。 –