swift - Alamofire API 连接和 Swift

标签 swift alamofire

我在向 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/

相关文章:

ios - 给图像添加参数;上传API如 "userid"= "50"

json - 无法通过可编码的swift从json中获取数据

Swift 3 上传视频到 Cloudinary 失败

objective-c - 在 Swift 中创建等效的 Objective-C Getter 和 Setter

ios - swift 音频套件 : How to build an VCO who's pitch is controlled by an AR-envelope?

parameters - 如何在 Alamofire, swift 3 中将字典作为参数发送

swift 4 : Unable to parse JSON response

swift - 苹果系统。如何以编程方式获取 CPU 温度

ios - 使用 Alamofire 将图像加载到 UITableViewCell 并圆角

swift - allHeaderFields 在查找键时不区分大小写