1

我正在尝试使用带有 http post 请求的 Mailjet API v3 发送电子邮件,但收到错误 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()
4

1 回答 1

1

标题错了...

我在做 :

request.setValue("Authorization", forHTTPHeaderField: "Basic <keysInBase64>")

代替 :

request.setValue("Basic <keysInBase64>", forHTTPHeaderField: "Authorization")

使用@LouFranco 建议的Charles Proxy,我能够找到错误。

于 2017-10-14T07:50:04.480 回答