Search code examples
swiftrestful-authentication

Can't authenticate with Api Key in Header


im trying to access a restful api but I can't seem to get the authorization header to work It works in curl. here's the documentation

var request = URLRequest(url: URL(string: "https://api-eu.dhl.com/track/shipments?trackingNumber=JJD000390011905333749")!)
request.addValue("DHL-API-Key: bliblablubbnotrealkey", forHTTPHeaderField: "Authorization")
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

URLSession.shared.dataTask(with: request) { data, response, error in
    print(response!)
    do {
        let json = try JSONSerialization.jsonObject(with: data!) as! Dictionary<String, AnyObject>
        print(json)
    } catch {
        print("error")
    }
}.resume()

Solution

  • request.AddHeader("DHL-APIKey", "APIKey-Value");