无法将类型[[String:String]'的值转换为预期的参数类型'HTTPHeaders?'

问题描述 投票:0回答:2

[我正在尝试使用MailgunAlarmofire从我的iOS应用发送邮件,我发现this piece of codeXcode产生错误:

无法将类型'[String:String]'的值转换为预期的参数输入“ HTTPHeaders?”

代码:

let parameters = [
                   "from": "[email protected]",
                     "to": "[email protected]",
                "subject": "Subject of the email",
                   "text": "This is the body of the email."]
    let header = [
            "Authorization": "Basic MY-API-KEY",
            "Content-Type" : "application/x-www-form-urlencoded"]

    let url = "https://api.mailgun.net/v3/MY-DOMAIN/messages"
    Alamofire.request(url,
                   method: .post,
               parameters: parameters,
                 encoding: URLEncoding.default,
                  headers: header)
            .responseJSON { response in
                print("Response: \(response)")
                }

有什么建议吗?

swift alamofire mailgun
2个回答
1
投票

您需要显式设置类型。

let headers : HTTPHeaders = [
    "Authorization": "Basic MY-API-KEY",
    "Content-Type" : "application/x-www-form-urlencoded"
]

-1
投票

尝试

let header:[String:Any] = [
        "Authorization": "Basic MY-API-KEY",
        "Content-Type" : "application/x-www-form-urlencoded"]
© www.soinside.com 2019 - 2024. All rights reserved.