Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve Alamofire response header for a request

Tags:

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!

like image 609
qbo Avatar asked Apr 09 '16 22:04

qbo


People also ask

How do I get response header responses?

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.

How do I find my server response header?

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.

How do you set a response header?

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.

What are API response headers?

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.


1 Answers

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 } 
like image 68
kamwysoc Avatar answered Sep 21 '22 23:09

kamwysoc