gpt4 book ai didi

ios - 使用 Alamofire 的授权 header

转载 作者:行者123 更新时间:2023-11-29 00:17:36 25 4
gpt4 key购买 nike

我正在使用一个 API,它将授权 key 放入 header 中,它在 Postman 中工作正常:

Postman snippet

这是我的代码:

let url = Requests.listCities(withLanguage: .en)
let headers: HTTPHeaders = [ "Authorization": "pmJAdo5N26WW74kCEy6RRvIdCScFCbAtKc2o0FNy"]
Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers)
.validate(contentType: [MIMEType.JSON])
.responseJSON {response in
completion(response)
}

响应是:

SUCCESS: {
error = Unauthorized;
status = failed;
}

我是否做错了,或者请求期间省略了授权

这是请求调试:

$ curl -v \
-H "Accept-Language: en;q=1.0, ar-US;q=0.9" \
-H "Authorization: pmJAdo5N26WW74kCEy6RRvIdCScFCbAtKc2o0FNy" \
-H "User-Agent: skinup/1.0 (appname; build:1; iOS 10.3.1) Alamofire/4.5.0" \
-H "Accept-Encoding: gzip;q=1.0, compress;q=0.5"

最佳答案

您可以将 Alamofire 的 header 设置到 URLRequest 中。所以你可以这样做:

    var urlRequest = URLRequest(url: URL(string: "your URL")!)
urlRequest.httpMethod = HTTPMethod.get.rawValue
urlRequest = try! URLEncoding.default.encode(urlRequest, with: nil)
urlRequest.setValue("pmJAdo5N26WW74kCEy6RRvIdCScFCbAtKc2o0FNy", forHTTPHeaderField: "Authorization")

Alamofire.request(urlRequest).responseJSON(completionHandler: {
response in

//code below
completion(response)
})

也许它会解决你的问题;)

关于ios - 使用 Alamofire 的授权 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44908117/

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