swift 中的 Http POST 请求通过 Mailjet 发送电子邮件
Http POST request in swift to send an email via Mailjet
我正在尝试使用 Mailjet API v3 发送带有 http post 请求的电子邮件,但收到错误 400。
我在 Javascript 中使用了完全相同的主体,但我猜错误 400 与它有关...
有什么想法吗?
var recipients = [Any]()
recipients.append(["Email": "email@gmail.com"])
var body: [String: Any] = [
"FromEmail": "anEmail@gmail.com",
"FromName": "Me",
"Subject": "YEEES",
"Text-part": "Greetings from IOS ;)",
"Recipients": recipients
]
var request = URLRequest(url: self.apiURL)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Authorization", forHTTPHeaderField: "Basic <keysInBase64>")
do {
request.httpBody = try JSONSerialization.data(withJSONObject: body, options: [])
}
catch {
print("error during JSON serialization")
dump(error)
return
}
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
print(error)
print(response)
print(data)
})
task.resume()
Headers错了...
我在做 :
request.setValue("Authorization", forHTTPHeaderField: "Basic <keysInBase64>")
而不是:
request.setValue("Basic <keysInBase64>", forHTTPHeaderField: "Authorization")
按照@LouFranco 的建议使用 Charles Proxy,我找到了错误。
我正在尝试使用 Mailjet API v3 发送带有 http post 请求的电子邮件,但收到错误 400。
我在 Javascript 中使用了完全相同的主体,但我猜错误 400 与它有关...
有什么想法吗?
var recipients = [Any]()
recipients.append(["Email": "email@gmail.com"])
var body: [String: Any] = [
"FromEmail": "anEmail@gmail.com",
"FromName": "Me",
"Subject": "YEEES",
"Text-part": "Greetings from IOS ;)",
"Recipients": recipients
]
var request = URLRequest(url: self.apiURL)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Authorization", forHTTPHeaderField: "Basic <keysInBase64>")
do {
request.httpBody = try JSONSerialization.data(withJSONObject: body, options: [])
}
catch {
print("error during JSON serialization")
dump(error)
return
}
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
print(error)
print(response)
print(data)
})
task.resume()
Headers错了...
我在做 :
request.setValue("Authorization", forHTTPHeaderField: "Basic <keysInBase64>")
而不是:
request.setValue("Basic <keysInBase64>", forHTTPHeaderField: "Authorization")
按照@LouFranco 的建议使用 Charles Proxy,我找到了错误。