我正在尝试在 Swift 中使用 Alamofire 发出带有 header 的发布请求。但是,我不断收到 extra parameter in method call 错误。我使用的是 4.5 版的 Alamofire。我无法找出错误。
请找到附件中的代码
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
}
}
关于swift - 带 header 的 Alamofire POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47775600/