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): ... }
Thanks in advance!
View headers with browser development tools Select the Network tab. You may need to refresh to view all of the requests made for the page. Select a request to view the corresponding response headers.
To view the request or response HTTP headers in Google Chrome, take the following steps : In Chrome, visit a URL, right click , select Inspect to open the developer tools. Select Network tab. Reload the page, select any HTTP request on the left panel, and the HTTP headers will be displayed on the right panel.
Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name.
HTTP Headers are an important part of the API request and response as they represent the meta-data associated with the API request and response. Headers carry information for: Request and Response Body. Request Authorization. Response Caching.
If the response is type of NSHTTPURLResponse
you can get header from response.allHeaderFields
.
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) }
As apple documentation says :
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.
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With