如何检索请求的Alamofire响应标头 [英] How to retrieve Alamofire response header for a request

查看:195
本文介绍了如何检索请求的Alamofire响应标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检索请求的响应标头?以下是我发出的请求。

how can I retrieve response headers for a request? Below is a request I make.

Alamofire.request(.GET, requestUrl, parameters:parameters, headers: headers)
        .responseJSON { response in switch response.result {
        case .Success(let JSON):

            ...

        case .Failure(let error):

            ...

    }

推荐答案

如果响应的类型为 NSHTTPURLResponse ,则您可以!可以从 response.allHeaderFields 中获取标头。

If the response is type of NSHTTPURLResponse you can get header from response.allHeaderFields.

因此,当您使用Alamofire responseJSON时,可以像这样访问NSHTTPURLResponse属性:

So when you use Alamofire responseJSON you can access to NSHTTPURLResponse property like this :

Alamofire.request(.GET, requestUrl, parameters:parameters, headers: headers).responseJSON {
        response in
        print(response.response?.allHeaderFields)
}

作为苹果文档说:


nary包含服务器响应中收到的所有HTTP标头字段。通过检查此词典,客户端可以看到HTTP服务器返回的原始标头信息。

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.

此词典中的键是标头字段名称,是从服务器接收到的。有关常用的HTTP标头字段的列表,请参见RFC 2616。

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.

因此,例如要获取响应标头中的内容类型,您可以以这种方式访问​​它:

So to get for example a content-type in response header you can access it in that way :

if let contentType = response.response?.allHeaderFields["Content-Type"] as? String {
        // use contentType here
}

这篇关于如何检索请求的Alamofire响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆