2

When i run this code it will give me this error:

2018-05-03 18:06:05.605035+0200 FNStats[4225:1826491] Task .<1> HTTP load failed (error code: 100 [1:100]) 2018-05-03 18:06:05.605179+0200 FNStats[4225:1826493] Task .<1> finished with error - code: 100

Can someone explaine to me what I am doing wrong!

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()
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Dani Kemper
  • 343
  • 2
  • 10
  • 1
    Are you setting the HTTPHeaderField correctly? Looks like the same string is both value and key. – Yarn May 03 '18 at 16:14
  • I am not sure. The documentation says: To make use of our APIs we require you to use an API Key. To use the API key you need to pass it along as a header with your requests. TRN-Api-Key: c400d2a8-8a99-45e7-a62b-3cef2d74ce14 . https://fortnitetracker.com/site-api – Dani Kemper May 03 '18 at 16:16
  • this solution worked for me - https://stackoverflow.com/a/52868844/5032981 – Prashant Gaikwad Oct 18 '18 at 07:19

1 Answers1

1

This worked for me. The header field is "TRN-Api-Key" and the value for that field is your 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()
Yarn
  • 238
  • 1
  • 12