我正在使用Alamofire,并且有一个类似curl的命令:
curl "https://abc.mywebsite.com/obp/v3.1.0/my/page/accounts/myaccount1/account" -H 'Authorization: DirectLogin token="eyJhbGciOiJIUzI1NiIsInR5cCI6wkpeVeCJr.eyIiOiIifQ.MV-E150zMCrk6VrWv"' -H 'Content-Type: application/json'
此命令在命令行上工作正常,并且我成功接收到响应。
对于Swift,我在网上找不到的帮助很少,因此在这里发布了一个问题,如何使用Swift拨打电话?
理想情况下,我想使用Alamofire,因为那是我进行所有网络通话所使用的。
我有类似的内容,但是它不起作用,它会显示“用户未授权”错误,这意味着它正在连接到服务器,但没有正确发送参数。
let url = "https://abc.mywebsite.com/obp/v3.1.0/my/page/accounts/myaccount1/account"
let loginToken = "'Authorization' => 'DirectLogin token=\"eyJhbGciOiJIUzI1NiIsInR5cCI6wkpeVeCJr.eyIiOiIifQ.MV-E150zMCrk6VrWv\"', 'Content-Type' => 'application/json'"
@IBAction func callAPIAction(_ sender: Any) {
Alamofire
.request(
self.url,
parameters: [
"token" : self.loginToken
]
)
.responseString {
response in
switch response.result {
case .success(let value):
print("from .success \(value)")
case .failure(let error):
print(error)
}
}
}
答案 0 :(得分:2)
您似乎想将标题Authorization
设置为DirectLogin token="eyJhbGciOiJIUzI1NiIsInR5cCI6wkpeVeCJr.eyIiOiIifQ.MV-E150zMCrk6VrWv"
。您可以这样做:
let loginToken = "DirectLogin token=\"eyJhbGciOiJIUzI1NiIsInR5cCI6wkpeVeCJr.eyIiOiIifQ.MV-E150zMCrk6VrWv\""
...
@IBAction func callAPIAction(_ sender: Any) {
Alamofire
.request(
self.url,
headers: [
"Authorization": self.loginToken,
"Content-Type": "application/json"
]
)
.responseString {
response in
switch response.result {
case .success(let value):
print("from .success \(value)")
case .failure(let error):
print(error)
}
}
}
...