gpt4 book ai didi

swift - 如何使用 Alamofire 将 CURL 请求转换为 Swift?

转载 作者:可可西里 更新时间:2023-11-01 01:08:04 24 4
gpt4 key购买 nike

我正在使用 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)
}
}
}

最佳答案

您似乎想将 header 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)
}
}
}

...

关于swift - 如何使用 Alamofire 将 CURL 请求转换为 Swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53249317/

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