我试图在
Swift中使用Alamofire发出带有标题的帖子请求.但是,我一直在方法调用错误中获取额外参数.我正在使用Alamofire的4.5版本.我无法弄清楚错误.
请找到附带的代码
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,headers: headers).responseJSON { (response:DataResponse) in switch(response.result) { case .success(let value):
//为Json序列化添加成功:
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 } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。