【问题标题】:Can't authenticate with Api Key in Header无法使用 Header 中的 Api Key 进行身份验证
【发布时间】:2020-01-13 03:02:33
【问题描述】:

我试图访问一个宁静的 api,但我似乎无法让授权标头工作 它适用于卷曲。 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()

【问题讨论】:

  • 你能修复文档链接吗?或者复制/粘贴等效的 curl 命令?
  • 哦,对不起。不知道那里发生了什么。我更新了它:developer.dhl/api-reference/shipment-tracking
  • Doc & sample curl -X GET 'https://api-eu.dhl.com/track/shipments?trackingNumber=7777777770' -H 'DHL-API-Key:PasteHere_ConsumerKey''DHL-API-Key:PasteHere_ConsumerKey' 的两个分号后不显示空格

标签: swift restful-authentication


【解决方案1】:
request.AddHeader("DHL-APIKey", "APIKey-Value");

【讨论】:

    猜你喜欢
    • 2014-05-12
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多