Swift:HTTP加载失败(错误代码:100)

时间:2018-05-03 16:08:01

标签: json swift url

当我运行此代码时,它会给我这个错误:

  

2018-05-03 18:06:05.605035 + 0200 FNStats [4225:1826491]任务   &LT 1为卤素; HTTP加载失败(错误   代码:100 [1:100])2018-05-03 18:06:05.605179 + 0200   FNStats [4225:1826493]任务。< 1>   完成错误 - 代码:100

有人可以向我解释我做错了什么!

let urlString = "https://api.fortnitetracker.com/v1/profile/pc/Danikemper010"
let url = NSURL(string: urlString)!
var request = URLRequest(url: url as URL)
request.setValue("TRN-Api-Key: c400d2a8-8a99-45e7-a62b-3cef2d74ce14", forHTTPHeaderField: "TRN-Api-Key: c400d2a8-8a99-45e7-a62b-3cef2d74ce14")

URLSession.shared.dataTask(with: request) { data, response, error in
    if let responseData = data
    {
        do{
            let json = try JSONSerialization.jsonObject(with: responseData, options: JSONSerialization.ReadingOptions.allowFragments)
            print(json)
        }catch{
            print("Could not serialize")
        }
    }
}.resume()

1 个答案:

答案 0 :(得分:0)

这对我有用。标题字段是" TRN-Api-Key"并且该字段的值是您的关键。

let urlString = "https://api.fortnitetracker.com/v1/profile/pc/Danikemper010"
let url = NSURL(string: urlString)!
var request = URLRequest(url: url as URL)
request.setValue("c400d2a8-8a99-45e7-a62b-3cef2d74ce14", forHTTPHeaderField: "TRN-Api-Key")

let task = URLSession.shared.dataTask(with: request) { data, response, error in

     if let responseData = data {

        print("We have some data")

        do{
            let json = try JSONSerialization.jsonObject(with: responseData, options: JSONSerialization.ReadingOptions.allowFragments)
            print(json)
        }catch{
            print("Could not serialize")
        }
    }            
}
task.resume()