gpt4 book ai didi

swift - Alamofire API 连接和 Swift

转载 作者:行者123 更新时间:2023-11-30 12:17:24 25 4
gpt4 key购买 nike

我在向 EatStreet API 发出 AlamoFire 请求时遇到问题。我有一个 API key ,并且已正确导入 Alamofire。

https://developers.eatstreet.com/endpoint/search

下面是网站的链接,他们将在其中为您创建 API URL 字符串。

尽管如此,我仍然没有成功

这是示例字符串

curl -X GET \
-H 'X-Access-Token: __API_EXPLORER_AUTH_KEY__' \
'https://api.eatstreet.com/publicapi/v1/restaurant/search?latitude=40.718293&longitude=-74.002276&method=pickup&pickup-radius=2'

下面是我的示例代码以及我的 api 和 Alamofire 请求。它不断返回 false 的结果值。感谢任何帮助

import UIKit
import Firebase
import FirebaseDatabase

import FirebaseAuth
import Alamofire
import SwiftyJSON

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(LoginViewController.dismissKeyboard))

//Uncomment the line below if you want the tap not not interfere and cancel other interactions.
tap.cancelsTouchesInView = false

view.addGestureRecognizer(tap)

let apiToContact = "\\ -H 'X-Access-Token: f6565a0360167144' \\'https://api.eatstreet.com/publicapi/v1/restaurant/search?latitude=40.718293&longitude=-74.002276&method=pickup&pickup-radius=2'"
Alamofire.request(apiToContact).validate().responseJSON() { response in
switch response.result {
case .success:
if let value = response.result.value {
let json = JSON(value)

// Do what you need to with JSON here!
// The rest is all boiler plate code you'll use for API requests
print(json)

}
case .failure(let error):
print(error)
}
}


}


//Calls this function when the tap is recognized.
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

最佳答案

Alamofire Docs 中所述,您应该发送带有 header 参数的请求

let headers: HTTPHeaders = [
"X-Access-Token": "API_EXPLORER_AUTH_KEY"
]

let url = "https://api.eatstreet.com/publicapi/v1/restaurant/search?latitude=40.718293&longitude=-74.002276&method=pickup&pickup-radius=2"

Alamofire.request(url, headers: headers).responseJSON { response in
print(response)
}

关于swift - Alamofire API 连接和 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45227571/

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