-1

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()
nOk
  • 2,725
  • 4
  • 13
  • 26
  • Could you fix the doc link? Or copy/paste the curl command equivalent you have? – Larme Sep 11 '19 at 09:47
  • Oh sry. don't know what happened there. I updated it: https://developer.dhl/api-reference/shipment-tracking – nOk Sep 11 '19 at 09:48
  • Doc & sample `curl -X GET 'https://api-eu.dhl.com/track/shipments?trackingNumber=7777777770' -H 'DHL-API-Key:PasteHere_ConsumerKey'` show not space after the two semicolons of `'DHL-API-Key:PasteHere_ConsumerKey'` – Larme Sep 11 '19 at 09:56

1 Answers1

0
request.AddHeader("DHL-APIKey", "APIKey-Value");
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103