我正在尝试使用以下alamofire请求命中API,并将标头作为参数传递。但它没有发生
let url = "http://192.168.1.42/api/v1/userConfig/8b5dbf7d9bdd40d29472be44efa9b7d3"
let secondTok: String = String(Token.get())
let bear = "Bearer " + secondTok
let param = ["Content-Type" : "application/json","Accept" : "application/json", "Authorization" : bear]
Alamofire.request(url, method: .get , parameters: param , encoding: JSONEncoding.default).responseJSON { response in
答案 0 :(得分:1)
在您的代码中,您正在制作参数。将其设置为标题
let url = "http://192.168.1.42/api/v1/userConfig/8b5dbf7d9bdd40d29472be44efa9b7d3"
let secondTok: String = String(Token.get())
let bear = "Bearer " + secondTok
let headers = ["Content-Type" : "application/json","Accept" : "application/json", "Authorization" : bear]
Alamofire.request(url, method: .get , headers: param , encoding: JSONEncoding.default).responseJSON { response in
答案 1 :(得分:0)
与标题授权一样使用此类
let url = "http://192.168.1.42/api/v1/userConfig/8b5dbf7d9bdd40d29472be44efa9b7d3"
let headers: HTTPHeaders = [
"Authorization": "Bearer " + secondTok!,
"Accept": "application/json"
]
Alamofire.request(url, method: .post, parameters: "" as? [String : AnyObject], encoding: JSONEncoding.default, headers: headers).responseJSON { response in
答案 2 :(得分:0)
Alamofire有接收标头作为参数的方法。使用它而不是将header放在参数内。
public func request(
_ url: URLConvertible,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil)
-> DataRequest
答案 3 :(得分:0)
使用get方法的Alamofire
>>> print('abcd\b')
abcd
>>> print('abcd\b ')
abc