gpt4 book ai didi

json - SwiftyJSON : How can I add token?

转载 作者:行者123 更新时间:2023-11-30 13:18:39 30 4
gpt4 key购买 nike

我正在使用 API 并通过 SwiftyJSON 获取 json 数据。我需要 API 的添加 token 。我如何使用 SwiftyJson 来做到这一点?

我的代码:

let jsonData = (NSData(contentsOfURL: NSURL(string: "http://api.football-data.org/v1/soccerseasons/424/leagueTable ")!)! as NSData)

    var readableJSON = JSON(data: jsonData, options: .MutableContainers, error: nil)

let name = readableJSON["standings"]

通常,当我使用 Swift 的 JSON 时,我会使用此代码添加 token :

    let url = NSMutableURLRequest(URL: NSURL(string: "http://api.football-data.org/v1/soccerseasons/424/leagueTable")!)
url.addValue("mytokenishere", forHTTPHeaderField: "X-Auth-Token")
url.HTTPMethod = "GET"

最佳答案

您是否正在使用此数据进行发布/发布?那是有道理的。我想您已经发出了获取包含该内容的可读数据“jsonData”的请求。由于您表明您还没有 json 数据,因此这可能会起作用。

var url = NSMutableURLRequest(URL: NSURL(string: "http://api.football-data.org/v1/soccerseasons/424/leagueTable")!)
url.addValue("mytokenishere", forHTTPHeaderField: "X-Auth-Token")
url.HTTPMethod = "GET"
NSURLSession.sharedSession().dataTaskWithRequest(url, completionHandler: data, response, error in {
if error == nil {
var readableJSON = JSON(data: data, options: .MutableContainers, error: nil)
let name = readableJSON["standings"]
url.HTTPBody = try! name.rawData()
NSURLSession.sharedSession().dataTaskWithRequest(url, completionHandler: data, response, error in {
//do something with his response from getting the data
})
} else {
print(error)
}
})

这是一种很奇怪的方法,但我认为这就是你想要的

关于json - SwiftyJSON : How can I add token?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37980746/

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