AFNetworking 2.0 // How to read response header

a slightly more robust code than Viruss mcs's:

if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) {
    NSHTTPURLResponse *r = (NSHTTPURLResponse *)task.response;
    NSLog(@"%@" ,[r allHeaderFields]);
}

returns

{
    Connection = "Keep-Alive";
    "Content-Length" = 12771;
    "Content-Type" = "application/json";
    Date = "Fri, 06 Dec 2013 10:40:48 GMT";
    "Keep-Alive" = "timeout=5";
    "Proxy-Connection" = "Keep-Alive";
    Server = "gunicorn/18.0";
}

similarly you can assure the casting is done right with [response respondsToSelector:@selector(allHeaderFields)], but you should also call that before you do the cast

if ([task.response respondsToSelector:@selector(allHeaderFields)]) {
    NSHTTPURLResponse *r = (NSHTTPURLResponse *)task.response;
    NSLog(@"%@" ,[r allHeaderFields]);
}

or no cast at all:

if ([task.response respondsToSelector:@selector(allHeaderFields)]) {
    NSLog(@"%@" ,[task.response performSelector:@selector(allHeaderFields)]);
}

Have you tried to get headers from NSURLResponse which is return,

You can try something like with NSURLResponse object,

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ([httpResponse respondsToSelector:@selector(allHeaderFields)]) {
    NSDictionary *dictionary = [httpResponse allHeaderFields];
    NSLog([dictionary description]);
}

Hope This will Help You.!


I subclassed AFHTTPRequestOperationManager and use the:

- (AFHTTPRequestOperation *)POST:(NSString *)URLString
                      parameters:(NSDictionary *)parameters
                         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

method for most of my web-service requests. When using that method, the response headers will be part of the operation object. Something like this:

[self POST:url parameters:newParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // Response headers will be a dictionary
    NSDictionary *headers = operation.response.allHeaderFields;
...

It's interesting that the above responses indicate that the parameter id responseObject returns an NSURLResponse. I'm running a JAX-RS server on the backend and I get a different response. When executing a curl command against my server, my response is this:

$ curl -v "http://10.0.1.8:3000/items"
* About to connect() to 10.0.1.8 port 3000 (#0)
*   Trying 10.0.1.8...
* Adding handle: conn: 0x7f9f51804000
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7f9f51804000) send_pipe: 1, recv_pipe: 0
* Connected to 10.0.1.8 (10.0.1.8) port 3000 (#0)
> GET /items HTTP/1.1
> User-Agent: curl/7.30.0
> Host: 10.0.1.8:3000
> Accept: */*
>
< HTTP/1.1 200 OK
< ETag: af0057e2-1c6d-4a47-b81a-a754238b60fd
< Content-Type: application/json
< Content-Length: 255
< Connection: keep-alive
<
* Connection #0 to host 10.0.1.8 left intact
[{"name":"Item1","uid":"F465AAD2-AA39-4C33-A57A-F0543C25C476"},
 {"name":"Item2","uid":"6505A82E-A473-4A7D-BC4B-BCBEFFFE8E9C"}]

My responseObject is an array of the items in the body of the server response and not an NSURLResponse. Here's how I retrieved the headers:

void (^handleSuccess)(NSURLSessionDataTask *, id) = ^(NSURLSessionDataTask *task, id responseObject) {
    // handle response headers
    NSHTTPURLResponse *response = ((NSHTTPURLResponse *)[task response]);
    NSDictionary *headers = [response allHeaderFields];

    // handle response body
    NSArray *responseItems = responseObject;
    for (NSDictionary *item in responseItems) {
        [self.activeDataController createObject:item];
    }
};