gpt4 book ai didi

ios - alamofire header + 参数

转载 作者:搜寻专家 更新时间:2023-11-01 05:50:41 25 4
gpt4 key购买 nike

Hello World ,

我来找你是因为,我尝试在同一个函数中发送带有参数的 header ,如下所示:

Alamofire.Manager.sharedInstance.request(.PUT, "url", headers: headers, parameters: parameters)

但也许您已经知道只发送 header 。

我也这样试过:

 let manager = Alamofire.Manager.sharedInstance
manager.session.configuration.HTTPAdditionalHeaders = [
"Authorization": token]

manager.request(.PUT, "http://192.168.99.100:3030/users/\(identity)", parameters: parameters, encoding:.JSON)

但是没有发送标题..

在 alamofire 中实现 header 的简单方法是什么?

提前致谢;-)

问候,

最佳答案

像其他参数一样在字典中设置标题并将其传递到标题中。例如

let Auth_header    = [ "Authorization" : token ]

Alamofire.Manager.sharedInstance.request(.PUT, "url", headers: Auth_header, parameters: parameters)

可以查看详情HTTP Basic Authentication

HTTP 基本身份验证

Request 上的 authenticate 方法会在适当的时候自动向 NSURLAuthenticationChallenge 提供 NSURLCredential:

let user = "user"
let password = "password"

Alamofire.request(.GET, "https://httpbin.org/basic-auth/\(user)/\(password)")
.authenticate(user: user, password: password)
.responseJSON { response in
debugPrint(response)
}

根据您的服务器实现,授权 header 也可能是合适的:

 let user = "user"
let password = "password"

let credentialData = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentialData.base64EncodedStringWithOptions([])

let headers = ["Authorization": "Basic \(base64Credentials)"]

Alamofire.request(.GET, "https://httpbin.org/basic-auth/user/password", headers: headers)
.responseJSON { response in
debugPrint(response)
}

关于ios - alamofire header + 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35472917/

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