gpt4 book ai didi

swift - 带 header 的 Alamofire POST 请求

转载 作者:搜寻专家 更新时间:2023-10-31 08:17:20 25 4
gpt4 key购买 nike

我正在尝试在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com