我想在标题请求中获得X-Dem-Auth,并 swift 将其存储在我的应用程序中。
查看响应:
headers {
"Content-Length" = 95;
"Content-Type" = "application/json; charset=utf-8";
Date = "Fri, 15 Apr 2016 08:01:58 GMT";
Server = "Apache/2.4.18 (Unix)";
"X-Dem-Auth" = null;
"X-Powered-By" = Express;
最佳答案
如果响应是NSHTTPURLResponse
类型,则可以从response.allHeaderFields
获取 header
正如苹果文件所说:
因此,例如要获取响应 header 中的X-Dem-Auth,您可以通过以下方式进行访问:
if let httpResponse = response as? NSHTTPURLResponse {
if let xDemAuth = httpResponse.allHeaderFields["X-Dem-Auth"] as? String {
// use X-Dem-Auth here
}
}
更新由于Evan R的评论而进行了更新
if let httpResponse = response as? HTTPURLResponse {
if let xDemAuth = httpResponse.allHeaderFields["X-Dem-Auth"] as? String {
// use X-Dem-Auth here
}
}
关于swift - 快速从请求响应中获取 header 数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36641700/