gpt4 book ai didi

swift - allHeaderFields 在查找键时不区分大小写

转载 作者:搜寻专家 更新时间:2023-10-31 08:30:30 24 4
gpt4 key购买 nike

我正在使用 Alamofire 进行 API 调用。

根据我使用的服务器,响应 header 可以大写。

但是正如 allHeaderFields 的文档所说:

var allHeaderFields: [AnyHashable : Any] { get }

A dictionary containing all the HTTP header fields received as part of the server’s response. By examining this dictionary clients can see the “raw” header information returned by the HTTP server.

The keys in this dictionary are the header field names, as received from the server. See RFC 2616 for a list of commonly used HTTP header fields.

HTTP headers are case insensitive. To simplify your code, certain header field names are canonicalized into their standard form. For example, if the server sends a content-length header, it is automatically adjusted to be Content-Length.

The returned dictionary of headers is configured to be case-preserving during the set operation (unless the key already exists with a different case), and case-insensitive when looking up keys.

For example, if you set the header X-foo, and then later set the header X-Foo, the dictionary’s key will be X-foo, but the value will taken from the X-Foo header.

但是在我的代码中,如果我这样做:

if let headers = response.response?.allHeaderFields {
print("Access-Token: \(response.response?.allHeaderFields["Access-Token"])")
print("access-token: \(response.response?.allHeaderFields["access-token"])")
print("access-token: \(response.response?.allHeaderFields["Access-token"])")
}

在我的控制台

Access-Token: nil
access-token: Optional(jdRtDzKHNs_i-jt3Lh3a3A)
access-token: nil

我错过了什么吗?

最佳答案

该错误与 Alamofire 无关。这是一个 Swift 错误。

不幸的是,这是 Swift 3 将 header 转换为字典时出现的已知错误。该漏洞自 2016 年以来一直存在,但至今仍未解决。更糟糕的是,他们甚至没有更正 Swift 文档。

这违反了 HTTP 规范,我不知道为什么他们只将其标记为中等优先级。似乎他们不关心也永远不会解决这个问题。

A workaround was made by Stephen Gurnett

Bug Report

关于swift - allHeaderFields 在查找键时不区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44324874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com