带标头的Alamofire POST请求 [英] Alamofire POST request with headers

查看:247
本文介绍了带标头的Alamofire POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Swift中使用Alamofire发出带有标头的发布请求.但是,我一直在方法调用中出现 extra 参数错误.我正在使用Alamofire的4.5版.我无法找出错误.

I am trying to make a post request with headers using Alamofire in Swift. However, I keep getting the extra parameter in method call error. I'm using version 4.5 of Alamofire. I'm unable to figure out the error.

请找到随附的代码

 let headers = ["Authorization": token, "Content-Type": "application/json"]

 Alamofire.request("http://localhost:8000/create", method: .post,  parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
}

推荐答案

以这种方式添加标题

    let headers = ["Authorization" : "Bearer "+accessToken!+"",
                   "Content-Type": "application/json"]



    Alamofire.request(URL, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON
            { (response:DataResponse) in
                switch(response.result)
                {
                case .success(let value):
//for Json serialization add in success:

    let JSON = try JSONSerialization.jsonObject(with: response.data! as Data, options:JSONSerialization.ReadingOptions(rawValue: 0))

                            guard let JSONDictionary: NSDictionary = JSON as? NSDictionary else {

                                return
                            }
                        completionHandler(JSONDictionary as? NSDictionary, nil)
                    case .failure(let error):
                        completionHandler(nil, error as NSError?)
                        break
                    }

            }

这篇关于带标头的Alamofire POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆