0

在我的应用程序中,在进行 API 调用时,我需要在标头中传递位置。但它的密钥被转换为 CamelCase。请在下面找到我的代码

let session = URLSession.shared
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "GET"

request.addValue("XYZLocation", forHTTPHeaderField: "location")

let task = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in

    if (error == nil){
       let str = String(data: data!, encoding: String.Encoding.utf8)
       print("Response : \(String(describing: str))")
    }
    else{
       print("Error : \(String(describing: error?.localizedDescription))")
    }
})
print("Header : \(String(describing: request.allHTTPHeaderFields))")
task.resume()

日志输出如下

Header : Optional(["Location": "XYZLocation"])

如何保持标题键区分大小写。请帮我。

4

0 回答 0